Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Questions about multiple inheritance #544

Closed
oovm opened this issue May 13, 2024 · 2 comments
Closed

Questions about multiple inheritance #544

oovm opened this issue May 13, 2024 · 2 comments

Comments

@oovm
Copy link

oovm commented May 13, 2024

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
Copy link
Author

oovm commented May 15, 2024

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

  • Collect all actual fields -> inline to generate struct fields
  • Collect all method fields -> inline to generate vtable
(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
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants