classes by value

Jonathan Marler johnnymarler at gmail.com
Thu Dec 14 16:10:17 UTC 2017


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[]`).



More information about the Digitalmars-d mailing list