www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Template specialization ignores attributes (at least 'immutable')

reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
parent reply Jason House <jason.james.house gmail.com> writes:
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
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
Jason House wrote:
 there is a related bugzilla entry for specialization with shared
 
 http://d.puremagic.com/issues/show_bug.cgi?id=3750
Thank 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, Ali
 
 
 
 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 16 2010