4 ms·
As a C++-lander, the fact that the struct/class dichotomy dictates boxing and polymorphism is mind boggling.
by gpderetta 6y ago
As a C++-lander, the fact that the struct/class dichotomy dictates boxing and polymorphism is mind boggling.
- josefx 6y agoIn C++ struct and class are the same except for default visibility rules. You could drop either keyword from the language and it wouldn't make any practical difference outside of C compatibility. In GCed languages the default seems to be that every object is referred to by pointer. Java took the "ugly" step of adding in primitive types so you could calculate 1 + 1 without causing dozens of cache misses. Struct types take this hack a step further. It generally makes the definition of the languages virtual machine a lot uglier, for example most jvm instructions operate on primitive types.
- Ygg2 6y agoWell I understand why it's there. Structs are just that data stored in the same layout. Classes are just a vtable pointer to some object on heap.
- mjburgess 6y agoIt's because "class" and "class" are referring to two difference semantics. An OO GC'd language "class" is a mechanism for run-time polymorphism. That's what it's for. C++ has no "runtime" concerns of these kinds, so virtualization sits on top systems with compile-time semantics (ie., c++ classes).
- gpderetta 6y agoBut that's just an implementation detail leaking. If the GC language properly supported interior pointers (and first class pointers) for the GC there wouldn't be a need to force boxing in all cases. You would be able to make the decision at the point of instantiation of the object. It would make the GC more complex, but I think these days most GCs support interior pointers for other reason anyway, right? Or am I missing something?
- dfgdghdf 6y ago> that's just an implementation detail leaking. I feel like this is every wart in the C++ language!
- xfer 6y agoAre you talking about go-style explicit pointers? I don't know if it is worth it to add them with a different semantics and dealing with nulls when you can have value types with an attribute. Pointers as an abstraction also leak implementation detail.
- gpderetta 6y agoPointers do not need to imply nullability (C++ references are not nullable for example). On the other hand having pointers allows implementing swap().
- xfer 6y agodotnet nowadays does have reference semantics. https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/byrefs https://docs.microsoft.com/en-us/dotnet/fsharp/language-refe...
- pjmlp 6y agoThat concept exists in languages older than C++, nothing mind boggling about it.
- brandmeyer 6y agoI agree with you, but I would rephrase it. As a C++-lander, I find it surprising that object allocation is tied to the object's type. IMO, the boxed/unboxed axis should be orthogonal to the type itself. It should be a property of the object instance, not of the object's type.
- gpderetta 6y agoYes, exactly. thanks for expressing this better.