3 ms·
How do generics change this? Can a type bound on a generic work like a static interface implementation specification, and recover the cost of wrapping the struc
by DougBTX 2y ago
How do generics change this? Can a type bound on a generic work like a static interface implementation specification, and recover the cost of wrapping the struct?
- erik_seaberg 2y agoYou can declare generic structs and generic functions, and get monomorphized copies (not erased to "any") of each function for all concrete types used. You can't look them up by reflection. You can't declare generic methods, and still can't overload methods by type.
- paullgdc 2y ago> get monomorphized copies (not erased to "any") of each function for all concrete types used That's not exactly true. You get one concrete function for all generic pointer types for instance. Generic functions take a hidden paramater called a dictionnary, which is fairly similar to vtable, since it contains type information and pointers for all methods of the generic type that the function calls. So methods on generic types are still performed through an indirection. https://github.com/golang/proposal/blob/master/design/generics-implementation-dictionaries-go1.18.md https://github.com/golang/proposal/blob/master/design/generi...
- erik_seaberg 2y agoThanks, had not seen that before. It's odd that they came close to reimplementing method dispatch for builtin types, but didn't surface it for developers.
- neonsunset 2y agoThat'd be how C#'s generics work for struct Ts. Go uses GC shape sharing: https://deepsource.com/blog/go-1-18-generics-implementation https://deepsource.com/blog/go-1-18-generics-implementation