Struct-typing in Phobos

Dicebot public at dicebot.lv
Tue Feb 4 13:07:00 PST 2014


On Tuesday, 4 February 2014 at 20:11:05 UTC, Chris Williams wrote:
> On Tuesday, 4 February 2014 at 13:01:28 UTC, Dicebot wrote:
>> I find structs completely superior to classes as design base 
>> and only resort to latter if find myself in need of 
>> polymorphic behavior.
>
> A person who is using Phobos has no option to switch to classes 
> when they find that they need polymorphic behavior, except by 
> copy-pasting into a new source file and revising the Phobos 
> code outside of Phobos.

"alias this" for the rescue again. You can a struct private 
member of a class and alias it. If you want to override its 
methods in class hierarchy you can generated wrapper methods in 
class via D reflection capabilities and override those.

But opposite is much harder, and issues with std.typecons.scoped 
show it.

> If there aren't really any speed or size advantages of 
> significance, then what are you considering to make structs 
> superior? (I ask since I would rather make an argument for 
> struct-typing in the article, so people understand the 
> decision.)

Speed advantage can be huge on certain types of programs. Each 
class is a reference type - this is first extra indirection. On 
top of that every non-final class call requires virtual table 
lookup - this is second extra indirection. And indirections can 
cost a lot.

I have also mentioned another important concern - you can 
easility turn struct into reference type by declaring pointer to 
struct. You can't easily turn class into value type.


More information about the Digitalmars-d mailing list