3 ms·
TIL > However, Go’s interface system works at runtime. The specific type of a value passed into an interface parameter is resolved while the program runs.
by dekdrop 2mo ago
TIL
> However, Go’s interface system works at runtime. The specific type of a value passed into an interface parameter is resolved while the program runs.
- mknyszek 2mo agoThere's a little more nuance to this. The compiler knows the static type of any value placed into an interface value, and emits code to place a concrete type pointer (or itab pointer) into the interface value. You can see this in the disassembly. Later when you pass this value around and assign it to places, sure, there may be a different type depending on where the interface value came from. But the construction of these values is very much static. In this sense I wouldn't really call this "resolv[ing] while the program runs" unless you take a very broad sense of "resolution."
- masklinn 2mo agoThe construction of interface values is a very shallow mean and essentially irrelevant to the need for interfaces. Whereas the runtime resolution of operations performed through an interface (dynamic dispatch) is the primary purpose of and use care for interfaces, with the dynamic resolution of wrapped types (type erasure / downcasts / type switches) is the secondary one.