Skip to content

Instantly share code, notes, and snippets.

@junlarsen
Created November 10, 2019 21:58
Show Gist options
  • Save junlarsen/537ba4d30d0e09021284619d1385ea04 to your computer and use it in GitHub Desktop.
Save junlarsen/537ba4d30d0e09021284619d1385ea04 to your computer and use it in GitHub Desktop.
/*
attempts to implement this
class A {
property num
method test() {
print("A")
}
}
class B {
property num
method test() {
print("B")
}
}
class C extends A, B {
property num
method test() {
super.A.test()
super.B.test()
print("C")
}
}
new C().test()
*/
interface MultiInheritableClass {
__parents: { [key: string]: MultiInheritableClass }
}
class A implements MultiInheritableClass {
__parents = {}
num = 0
test() {
return A_test(this)
}
}
class B implements MultiInheritableClass {
__parents = {}
num = 1
test() {
return B_test(this)
}
}
// class C extends A, B
class C implements MultiInheritableClass {
__parents = { A: new A, B: new B }
// .num must be defined because both A, B have a .num
num = 0
// same goes for this function
test() {
// Must pass by explicit reference, cannot spread
return C_test(this, this.__parents.A, this.__parents.B)
}
}
function C_test(thisRef: C, refA: A, refB: B) {
refA.test()
refB.test()
// Editing properties also works
refA.num++
console.log(refA.num)
console.log("C")
}
function B_test(thisRef: B) {
console.log("B")
}
function A_test(thisRef: A) {
console.log("A")
}
new C().test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment