string types: const(char)[] and cstring

Reiner Pope some at address.com
Sat May 26 23:02:56 PDT 2007


Walter Bright wrote:
> Reiner Pope wrote:
>> Will there be something in the type system which enables you to safely 
>> say, "This is the only reference to this data, so it's ok for me to 
>> make this invariant" ?
> 
> Safely? No. You will be able to explicitly cast to invariant, however, 
> the programmer will have to ensure it is safe to do so.
> 
>> Does 'scope' happen to have anything to do with that?
> 
> No. Scope just ensures that the reference does not 'escape' the scope 
> it's in.

I must have misunderstood what scope specifies. I had thought that, to 
avoid being escaped, scope specified that your variable may not be 
aliased by another (non-scope) name. In that case, I thought, can't you 
say: "well, when I leave this function, I'm the only one holding a 
reference to this data, so it would be safe to call it invariant (or 
anything else I choose)." I thought a compiler could have a special case 
saying, "at the end of scope, you can safely turn any scope variables 
into whatever you want".

However, I was surprised to find out that the following code compiled 
fine, although it returns a dead object:

Foo foo()
{
     scope Foo f = new Foo();
     Foo g = f;
     return g;
}


   -- Reiner



More information about the Digitalmars-d-announce mailing list