Go and Rust — objects without class
Go and Rust — objects without class
Posted May 21, 2013 6:01 UTC (Tue) by mmaroti (guest, #84368)Parent article: Go and Rust — objects without class
1) In Rust you separately pass the virtual table pointers with the object pointers. So if Rust wants to store a vector of objects implementing the Shape interface, then you have to record both the virtual table pointer and the data pointer for each Shape. However, Rust stores a vector of Circle objects that implement the Shape interface by storing a single virtual table for circle and a vector of data pointers for each Circle. Haskell does the same (there interfaces are called type classes and implementations are instances).
2) Go stores the virtual tables together with the objects. This is how C++ and Java stores objects, so no matter if you have a vector of Shapes or Circles, both can be stored in a vector of data pointers.
Posted May 22, 2013 8:19 UTC (Wed)
by neilbrown (subscriber, #359)
[Link] (3 responses)
In Rust a pointer to a value is usually just to the value - no vtable is implied. To get a vtable,you use the "as" operator. "mycircle as Shape" becomes a pair of pointers, one to 'mycircle', one to a vtable which implements "Shape" for mycircle.
In Go, a pointer to a value is just to that value, no vtable. To get a vtable you need to convert it to an 'interface' type, such as by "Shape(mycircle)". This will compute (possibly at runtime) the vtable if it doesn't already exist, and will create a pointer-pair, just like in Rust. In Go you don't need the explicit cast. Assigning to an interface-type or passing as a parameter where an interface-type is expected are sufficient. This is a small difference to Rust where I think the "as" is required (not sure though).
More details of the Go approach can be found in http://research.swtch.com/interfaces
Posted May 22, 2013 20:40 UTC (Wed)
by mmaroti (guest, #84368)
[Link] (2 responses)
http://smallcultfollowing.com/babysteps/blog/2012/04/09/r...
By these accounts you cannot do upcasts in Rust, so the vtables (the actual type of objects) cannot be computed at runtime. In Go, the vtables (actual type) of objects can be computed.
From my point of view it is an implementation detail whether a language is storing the vtable in the first field of the data object (the C++ way) or you pass the vtable pointer together with the data pointers (the Go way). The important point, that you try to cast from interface{} to any other interface.
By the way, does Go have polymorphic arrays, which would ensure that all objects in the array are of the exact same type, and only a single vtable pointer is stored together with a bunch of data pointers?
Posted May 22, 2013 23:04 UTC (Wed)
by neilbrown (subscriber, #359)
[Link] (1 responses)
Posted May 23, 2013 2:25 UTC (Thu)
by Cyberax (✭ supporter ✭, #52523)
[Link]
Go and Rust — objects without class
This is described in section "8.1.10 Object types" of the Rust reference manual, and seems to agree with what you said.
This seems quite different to your description of Go.
Go and Rust — objects without class
https://news.ycombinator.com/item?id=3749860
Go and Rust — objects without class
Go and Rust — objects without class
Reserve the first slot in the vtable for interface lookup function, kinda like QueryInterface in COM.