On 2010-02-08 00:27:29 -0500, Andrei Alexandrescu
<[email protected]> said:
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:
Are you saying by "you can take addresses of its member" that it's up
to the struct implementation to not leak the address of a member? (by
making members private and propagating access only through proxy scope
structs?)
I'd say that it looks useful, but it can't do much by itself. That's
just one piece of the puzzle and without other pieces it won't go very
far. Consider this:
scope struct Transitory(O) {
private O o;
this(string s) { o = new O(s); }
string toString() { return o.toString(); }
}
Transitory!MyObject t = Transitory!MyObject("hello");
t.toString();
This code has two issues:
1. MyObject's constructor could leak a reference.
2. MyObject's toString could leak a reference.
Do we need a "scope class" for this to work?
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.
I'm not sure why you need that. If Transitory is scope, then you can't
take tr's address. Taking the address of A doesn't give you the address
of tr (in reality it might be the same address, but it'll be typed as A
and won't allow you to get the address of tr anyway). So why does A
need to be scope?
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.
That'd be really great.
--
Michel Fortin
[email protected]
http://michelf.com/