Classes or stucts :: Newbie

Steven Schveighoffer schveiguy at yahoo.com
Mon Dec 20 10:41:15 PST 2010


On Sun, 19 Dec 2010 17:38:17 -0500, 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.
>
> The compiler can help, but it can't fix the problem any more that it can
> guarantee that a pointer to a local variable doesn't escape once you've  
> passed
> it to another function. In _some_ circumstances, it can catch escaping  
> pointers
> and references, but in the general case, it can't.
>
> If we have library solutions for people who want to play with fire,  
> that's fine.
> But scoped classes is just not one of those things that the language  
> really
> needs. They complicate things unnecessarily for minimal benefit.

I don't mind having a solution as long as there is a solution.

The main need I see for scoped classes is for when you *know* as the  
programmer that the lifetime of a class or struct will not exceed the  
lifetime of a function, but you don't want to incur the penalty of  
allocating on the heap.  Mostly this is because the functions you want to  
call take classes or interfaces.

It's difficult to find an example with Phobos since there are not many  
classes.  But with Tango, scoped classes are used everywhere.

-Steve


More information about the Digitalmars-d-learn mailing list