WebAssembly / gc

Branch of the spec repo scoped to discussion of GC integration in WebAssembly
https://webassembly.github.io/gc/
Other
982 stars 70 forks source link

Questions about multiple inheritance #544

Closed oovm closed 3 months ago

oovm commented 3 months ago

What should I do if I want a multi-inheritance gc language like python?

Are there any plans to introduce multiple inheritance and mro resolution (C3 linearization process)?

If such a feature cannot be added, what are the better simulation methods?

If I do

class Real(A, B) {
     _fake_a: A
     _fake_b: B
}

Real is B => self._fake_a is A or self._fake_b is B
A as Real => ??? //A and Real have no real subtype relationship and cannot be converted
B as Real => ??? //B and Real have no real subtype relationship and cannot be converted

Is this a good approach?

Or can this method simulate multiple inheritance?

https://github.com/WebAssembly/gc/blob/main/proposals/gc/Overview.md#objects-and-method-tables

oovm commented 3 months ago

Consider the following pseudo code

class Fly {
    fly: i32
    action() {}
}
class Walk {
    walk: f32
    action() {}
}
class Monster(Fly, Walk) {
    monster: f64
    action() {
        // selected by mro, in compile time
        super<Fly>.action()
    }
}

I tried

(type $fly (struct (ref $fly-vt) (field "fly" mut i32)))
(type $walk (struct (ref $walk-vt) (field "walk" mut f32)))
(type $monster (struct 
    (ref $monster-vt)
    (field "Fly.fly" mut i32)
    (field "Walk.walk" mut f32)
    (field "Monster.monster" mut f64)
))

(type $fly-vt (ref $action))
(type $walk-vt (ref $action))
(type $monster-vt (struct
    // (inline $fly-vt)
    // (inline $walk-vt)
    (ref $action)
))

But I still don't know how to determine the runtime subtype, or how to upcast or downcast a runtime type.

rossberg commented 3 months ago

GC types are not source language types, rather, they describe to the GC the memory layout of low-level data structures in a language runtime. On that level, multiple inheritance does not make sense, because memory layouts can only be extended in one dimension. Mechanisms like multiple inheritance require indirections, which need to be implemented in Wasm code, in the same way you would with linear memory or in a native compiler. This is expected behaviour.