On Thursday, 8 October 2015 at 08:21:09 UTC, Kagamin wrote:
On Tuesday, 6 October 2015 at 20:31:58 UTC, Jonathan M Davis wrote:
I don't think the problem is with structs. The problem is that programmers coming from other languages default to using classes. The default in D should always be a struct. You use a class because you actually need inheritance or because you want to ensure that a type is always a reference type and don't want to go to the trouble of writing a struct that way (and even then, you should probably just write the struct that way).

Hmm... If we must emulate reference semantics manually, it feels like C++ with explicit references, pointers and all sorts of smart pointers, and obviates need for classes being reference types: just emulate reference semantics as we must do it anyway.

Maybe, but having classes be value generally makes no sense, because you can't use polymorphism with value types. Classes are inherently reference types given their semantics. Even in C++ that's the case. It's just that they don't separate out classes and structs the way we do. But classes that use inheritance have to be used as reference types or all you're doing is sharing implementation (which can be done easily enough without inheritance). You're not doing anything with polymorphism without references. So, the separation that D has makes a lot of sense. It's just that in some cases - namely where determinstic destruction is required - having them be managed by the GC doesn't work, and we need a solution for that. Most classes work just fine with a garbage collector though.

And if we have classes that are inherently ref-counted or which are sitting inside of smart-pointers, then they're still reference types like they should be. They just have their lifetime managed in a more deterministic manner for those cases where that's appropriate.

- Jonathan M Davis

Reply via email to