Classes or stucts :: Newbie

Nick Voronin elfy.nv at gmail.com
Sun Dec 19 18:13:54 PST 2010


On Sun, 19 Dec 2010 17:26:20 -0800
Jonathan M Davis <jmdavisProg at gmx.com> wrote:

> On Sunday 19 December 2010 16:50:34 Nick Voronin wrote:
> > On Sun, 19 Dec 2010 14:38:17 -0800
> > 
> > Jonathan M Davis <jmdavisProg at gmx.com> wrote:
> > > On Sunday 19 December 2010 14:26:19 bearophile wrote:
> > > > Jonathan M Davis:
> > > > > There will be a library solution to do it, but again, it's unsafe.
> > > > 
> > > > It can be safer if the compiler gives some help. For me it's one of the
> > > > important unfinished parts of D.
> > > 
> > > Whereas, I would argue that it's completely unnecessary. structs and
> > > classes serve different purposes. There is no need for scoped classes.
> > > They may perodically be useful, but on the whole, they're completely
> > > unnecessary.
> > 
> > How do you define a need for scoped classes?
> > 
> > I see two aspects there: first is having destructor called at known point
> > rather than arbitrarily, second is performance.
> > 
> > It looks perfectly reasonable for me to want to have first. Do you know why
> > things which makes program act more deterministic are shunned from D? Does
> > it really add so much to safety that it is worth reducing programmer's
> > control?
> 
> Then use a struct.

Here is where we diverge. Choosing struct vs class on criteria of their placement makes no sense to me. Difference in default placement hardly matters at all, you can perfectly put structs in heap or in static segment yet still maintain same properties. It's those properties which matter when I choose one or another, not where it will reside in particular part of program.

> That's one of the reasons that they exist. And if you 
> _really_ want to make sure that a class' destructor gets run, you can always use 
> clear() (which is arguably an abomination in its own right).

This is what I don't understand. Fine. structs are meant for stack, class for heap. Conceptually, anyway. Still there will be ways to put class on stack and struct in heap. And there is(will be) clear(). Now I do see benefit of scope storage. It looks clean, it is supported by compiler meaning reasonable level of protection against misuse, it is extremely effective. What are benefits of _not having_ it?

-- 
Nick Voronin <elfy.nv at gmail.com>


More information about the Digitalmars-d-learn mailing list