"The last feature": scope structs

Lars T. Kyllingstad public at kyllingen.NOSPAMnet
Mon Feb 8 01:46:32 PST 2010


Andrei Alexandrescu wrote:
> Following work on typechecking synchronized methods, Walter and I were 
> discussing about managing escaping references to objects that wouldn't 
> actually agree to escaping. The problem is that today a value has no 
> idea that its address is being taken, so it has no saying in that.
> 
> To compensate for that, my idea was to allow @disable on opUnary!"&". 
> Walter suggested allowing "scope" as a property of a struct:
> 
> scope struct Transitory
> {
>     ...
> }
> 
> A scope struct cannot have its address taken. You can take addresses of 
> its members (access permitting), but you can't take the address of any 
> other struct that has at least one scope struct as a direct member. For 
> example:
> 
> struct A
> {
>     int x;
>     Transitory tr;
> }
> 
> A will also be scope. So scope has this funny way of propagating 
> outwards, as opposed to qualifiers, which propagate inwards.

Though the feature seems useful, I worry about the asymmetry with scope 
classes.  I think it will be very confusing to new D programmers that a 
scope class and a scope struct have very little to do with each other.

   - You can't have globals, statics, fields, or enums that
     are scope classes, but it seems they can be scope structs.
     (Hence, there can be no outward propagation of the scope
     class qualifier either.)

   - You can't take the address of a scope struct, but you can
     take the address of a scope class reference.

   - A scope class variable must be declared with scope, but
     your example indicates that that's not the idea for scope
     structs.


> This is as much as we have in terms of a design right now, so definitely 
> it stands some more fleshing out. If gotten right, scope should allow 
> defining a number of useful idioms, such as pass-down values, 
> unique/lent objects, and the such.

What are pass-down values?  (Yes, I've tried searching for it.)


> Working against escapes has long been a problem, and with scope we're 
> hoping to strike a balance between useful and simple.
> 
> Please share any thoughts you might have.

In brief: I like the idea, but I like the '@disable opUnary!"&"' 
suggestion better than 'scope struct'.

-Lars



More information about the Digitalmars-d mailing list