classes by value

Petar Petar
Fri Dec 15 14:06:27 UTC 2017


On Friday, 15 December 2017 at 12:56:41 UTC, Jonathan M Davis 
wrote:
> On Friday, December 15, 2017 11:10:42 Dgame via Digitalmars-d 
> wrote:
>> On Friday, 15 December 2017 at 09:18:23 UTC, Seb wrote:
>> > On Thursday, 14 December 2017 at 16:40:33 UTC, Petar Kirov
>> >
>> > [ZombineDev] wrote:
>> >> ```
>> >> scope foo = new Foo();
>> >> ```
>> >
>> > That's about to be deprecated in favor of scoped!Foo:
>> >
>> > https://dlang.org/deprecate.html#scope%20for%20allocating%20classes%20on %20the%20stack
>> Since scope was revived with DIP-1000 we will see about that. 
>> I doubt that the deprecation will stay.
>
> DIP 1000 is half killing it. DIP 1000 is using scope for 
> something very different, and it has nothing to do with putting 
> classes on the stack. However, as an optimization, it may put a 
> class on the stack if it determines that the the class object 
> is unique. Anyone who wants to guarantee it should be using 
> std.typecons.scoped.
>
> - Jonathan M Davis

No, DIP1000 only enhances the existing meaning of `scope` 
classes. It is not an optimization that may or may not happen. 
When you do `scope foo = new Foo()` and `Foo` is a class you 
always get stack allocation, regardless of whether you're 
compiling with `-dip1000` or not. It has been defined in the spec 
and implemented for many years now [1].
Many project rely heavily on that (e.g. [2][3]) and it will not 
be deprecated any time soon, regardless of whatever 
https://dlang.org/deprecate says.

[1]: 
https://github.com/dlang/dmd/commits/master/test/runnable/testscope.d
[2]: 
https://github.com/sociomantic-tsunami/ocean/search?utf8=%E2%9C%93&q=scope&type=
[3]: 
https://github.com/search?utf8=%E2%9C%93&q=scope+repo%3Adlang%2Fdmd+extension%3Ad+path%3Asrc%2Fdmd&type=Code&ref=advsearch&l=&l= (look for "scope " in scr/)


More information about the Digitalmars-d mailing list