classes by value

Dgame r.schuett.1987 at gmail.com
Fri Dec 15 11:11:24 UTC 2017


On Thursday, 14 December 2017 at 16:40:33 UTC, Petar Kirov 
[ZombineDev] wrote:
> On Thursday, 14 December 2017 at 16:10:17 UTC, Jonathan Marler 
> wrote:
>> On Thursday, 14 December 2017 at 14:45:51 UTC, Dgame wrote:
>>> Strongly reminds me of scoped
>>
>> Declaring a variable as `scoped` prevents that variable from 
>> escaping the scope it is declared in.  This restriction allows 
>> the compiler certain optimizations, such as allocating classes 
>> on the stack, i.e.
>> ```
>> scoped foo = new Foo();
>> ```
>>
>> this optimization is well-known to D programmers so class 
>> allocation on the stack is strongly associated with the 
>> "scoped" modifier which is probably why this "class by value" 
>> snippet reminds you of "scoped".
>>
>> Even though "classes by value" can be implied in certain 
>> usages of "scoped", "scoped" carries with it extra semantics 
>> that "classes by value" on it's own does not. This snippet 
>> allows "classes by value" on it's own, which enables different 
>> ways of using classes (for example, creating an array of 
>> classes by value `Value!MyClass[]`).
>
> I think what Dgame meant was: 
> https://dlang.org/phobos-prerelease/std_typecons#scoped. For 
> the built-in scoped classes, the keyword is 'scope', not 
> 'scoped': https://dlang.org/spec/class.html#auto.

Yes, that's what I meant.



More information about the Digitalmars-d mailing list