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.

So you can have both:

```
scope foo = new Foo();
```

and:
```
auto foo = scoped!Foo();
```

and finally:
```
scope foo = scoped!Foo();
```

Reply via email to