digitalmars.D - Template specialization ignores attributes (at least 'immutable')
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (27/27) Feb 15 2010 I've tried a function that reads an answer from the standard input:
- Jason House (3/42) Feb 15 2010 there is a related bugzilla entry for specialization with shared
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (7/55) Feb 16 2010 Thank you.
I've tried a function that reads an answer from the standard input:
T get_answer(T)(string question)
{
dout.writef(question, ": ");
T answer;
din.readf(&answer);
return answer;
}
That doesn't work for strings, as they are immutable and din.readf fails.
So I wrote a specialization for string, which uses the instantiation of
the same template for char[] and adds an .idup at the end:
T get_answer(T : string)(string question)
{
return get_answer!(char[])(question).idup;
}
That did not work, because even the get_answer!(char[]) call selects the
string specialization.
Then I realized that a compile time 'if' works:
T get_answer(T : string)(string question)
if (is (T == string)) // <--- NECESSARY
{
return get_answer!(char[])(question).idup;
}
That's cool but is a little silly, as it means "this is the
specialization for string AND consider it only when T is a string."
Should attributes be considered for specializations as well?
Ali
Feb 15 2010
there is a related bugzilla entry for specialization with shared
http://d.puremagic.com/issues/show_bug.cgi?id=3750
Ali Çehreli Wrote:
I've tried a function that reads an answer from the standard input:
T get_answer(T)(string question)
{
dout.writef(question, ": ");
T answer;
din.readf(&answer);
return answer;
}
That doesn't work for strings, as they are immutable and din.readf fails.
So I wrote a specialization for string, which uses the instantiation of
the same template for char[] and adds an .idup at the end:
T get_answer(T : string)(string question)
{
return get_answer!(char[])(question).idup;
}
That did not work, because even the get_answer!(char[]) call selects the
string specialization.
Then I realized that a compile time 'if' works:
T get_answer(T : string)(string question)
if (is (T == string)) // <--- NECESSARY
{
return get_answer!(char[])(question).idup;
}
That's cool but is a little silly, as it means "this is the
specialization for string AND consider it only when T is a string."
Should attributes be considered for specializations as well?
Ali
Feb 15 2010
Jason House wrote:there is a related bugzilla entry for specialization with shared http://d.puremagic.com/issues/show_bug.cgi?id=3750Thank you. Does anyone know how it should behave though? My specialization below should be sufficient, right? I shouldn't need to also use a constraint, right? Thank you, AliAli Çehreli Wrote:I've tried a function that reads an answer from the standard input: T get_answer(T)(string question) { dout.writef(question, ": "); T answer; din.readf(&answer); return answer; } That doesn't work for strings, as they are immutable and din.readf fails. So I wrote a specialization for string, which uses the instantiation of the same template for char[] and adds an .idup at the end: T get_answer(T : string)(string question) { return get_answer!(char[])(question).idup; } That did not work, because even the get_answer!(char[]) call selects the string specialization. Then I realized that a compile time 'if' works: T get_answer(T : string)(string question) if (is (T == string)) // <--- NECESSARY { return get_answer!(char[])(question).idup; } That's cool but is a little silly, as it means "this is the specialization for string AND consider it only when T is a string." Should attributes be considered for specializations as well? Ali
Feb 16 2010








=?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com>