Why string alias is invariant ?

Sergey Gromov snake.scaly at gmail.com
Wed Jan 30 15:03:58 PST 2008


Really, why ?  You can't assign a dynamically created data to a variable of type string.  You can't pass a dynamically created data into a function receiving string !  If you wanted efficiency, if you wanted to guarantee that anybody receiving string can receive a char literal without duplication,---make it const then.  Invariant casts to const(char)[] implicitly, so as char[].  And even then, I'd prefer plain old char[] for string and foo(in string s) for a function that guarantees not to change it.

Right now I'm having trouble calling std.file.listdir() just because it receives string.

P.S.  Many thought mus be put into choosing a return type for a library function.  Because if it returns a unique copy of data it must be char[] so that i'm free to modify it.  The const(char)[] or const(char[]) must only be used if the function is not sure if the returned result is unique.

SnakE



More information about the Digitalmars-d mailing list