Char literals

Eldar Insafutdinov e.insafutdinov at gmail.com
Fri Nov 6 03:24:51 PST 2009


I've just written a simple replicate function (the one from std.range does not work in CTFE):
T[] replicate(T)(int n, T value)
{
    T[] ret;
    if (n > 0)
    {
        for(int i = 0; i < n; i++)
            ret ~= value;
    }
    return ret;
}
it's not the best implementation, but that's not the thing I want to say today. When I call it

string str = replicate(5, 'a');

I get

moc.d(370): Error: cannot implicitly convert expression (replicate(5,',')) of type char[] to string

That means that char literals are "char", but not "immutable(char)". I remember the whole conversation about array literals, but char literals are closer to those of the string. What's the reasoning behind leaving them as mutable char?



More information about the Digitalmars-d mailing list