string types: const(char)[] and cstring
Chris Nicholson-Sauls
ibisbasenji at gmail.com
Sat May 26 23:02:00 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.
>
>> 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.
That's an interesting syntax, casting to a trait/attribute with the rest of the type
inferred. I presume cast(const) works as well. (Maybe cast(scope)? Then again, what's
the use...) Given cast(*) where * is invariant/const, is cast(*)T[] the same as
cast(*(T)[]) or cast(*(T[]))? That is, does the trait apply to the element type, or the
array?
-- Chris Nicholson-Sauls
More information about the Digitalmars-d-announce
mailing list