DIP1000: Scoped Pointers
Mike via Digitalmars-d-announce
digitalmars-d-announce at puremagic.com
Wed Aug 17 00:04:26 PDT 2016
On Wednesday, 17 August 2016 at 06:44:41 UTC, Mike wrote:
> On Wednesday, 17 August 2016 at 04:28:33 UTC, Rory McGuire
> wrote:
>
>> Basically DIP1000 makes it so that:
>>> void main()
>>> {
>>> A obj;
>>> {
>>> scope A a = new A(1);
>>> obj = a;
>>> }
>>> assert(obj.x == 1); // fails, 'a' has been destroyed
>>> }
>>
>> Will not compile.
>
> Ok, that makes sense. But then the feature illustrated on the
> deprecations page is wrong. I think what has been deprecated
> is `scope` as a type modifier, not `scope` as a storage class,
> but the example on the deprecations page illustrates the
> `scope` storage class, not the type modifier.
>
> I believe what the deprecations page intended to say was that
> this has been deprecated:
> scope class A
> {
> this(int x) { }
> }
>
> And DIP1000 finally implements the `scope` storage class
> properly:
> void main()
> {
> A obj;
> {
> scope A a = new A(1);
> obj = a; // compile-time error. Good!
> }
> assert(obj.x == 1); // or is it usage that triggers
> the compile-time error?
> }
>
> Mike
Or perhaps DIP1000 changes the current behavior of the `scope`
storage class.
My understanding is that the `scope` storage class currently
allocates a class on the stack (though its usage for this purpose
is deprecated in favor of std.typecons.scoped). If DIP1000 is
implemented, it will change that behavior, so the allocation will
instead be on the GC heap, but the compiler will do some
flow-control analysis to prevent escaping references. Is that
right?
Mike
More information about the Digitalmars-d-announce
mailing list