Why string alias is invariant ?

Janice Caron caron800 at googlemail.com
Thu Jan 31 09:50:41 PST 2008


On 1/31/08, Don Clugston <dac at nospam.com.au> wrote:
> Seems that assumeUnique is going to be so common, it deserves a shorter name.

Oddly enough,

    cast(string)s

is shorter than

    assumeUnique(s)

and achieves the same effect. :)  I think someone somewhere must have
decided that explicit casts are to be avoided. That said, assumeUnique
works for things that aren't strings, too. It's basically equivalent
to cast(invariant).

assumeUnique does have a side effect though, which is that its
parameter must be an lvalue, which assumeUnique nulls. So you couldn't
do

    string data = assumeUnique( read( "myfile.txt" ));

even if you wanted to. The "officially correct" way would be

    char[] temp = cast(char[]) read( "myfile.txt" );
    string data = assumeUnique(temp);
        /* temp is now null */

But that's way too much typing for me, so I'd just write:

    string data = cast(string) read("myfile.txt");



More information about the Digitalmars-d mailing list