4 ms·
> Go’s interface system works at runtime. The specific type of a value passed into an interface parameter is resolved while the program runs. This dynamic dispa
by tancop 2mo ago
> Go’s interface system works at runtime. The specific type of a value passed into an interface parameter is resolved while the program runs. This dynamic dispatch clashes with generics being resolved at compile time.
What if the compiler generated both vtable and monomorphized variants for each method? Most calls would use the static version but places that need it like interface generics call the virtual method.
- saturn_vk 2mo agovtables still need types, and a generic method that is not yet instantiated will not have them
- masklinn 2mo agoThe problem is that your vtable would have to have a monomorphised copy of the generic method for each possible instance, this requires whole program analysis just to generate the interface — and even then I would not be surprised if you could generate calls which the compiler was not able to relate to the interface — and then you get to the potential size explosion of your vtable as it has to hold an unbounded number of methods. Note that despite having much stronger static semantics and much weaker reflection than Go Rust has the exact same limitation: a trait method with a non-lifetime generic parameter is not dyn-compatible. C++ is the same, a template member function (generic method) can’t be virtual (dynamically dispatched).