string types: const(char)[] and cstring

Walter Bright newshound1 at digitalmars.com
Sat May 26 22:39:24 PDT 2007


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.

> invariant(char)[] createJunk()
> {
>     /* scope? */ char[] val = "aaaaa".dup;
>     size_t index = rand() % 5;
>     val[index] = rand();
> 
>     return cast(invariant(char)[]) val;
> }
> 
> I mean, do I really need to cast it to invariant there? It's easy to see 
> that there's only one copy of val's data in existance.

Easy for you to see, not so easy for the compiler to. And besides:

	return cast(invariant)val;

will do the trick more conveniently.



More information about the Digitalmars-d-announce mailing list