www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - template specialization for arrays

reply J Arrizza <cppgent0 gmail.com> writes:
--bcaec52161cf14621a04b0719a01
Content-Type: text/plain; charset=ISO-8859-1

I have a template that I'd like to have a specialization for arrays.
Initiall I need it to work for byte arrays, but I'd like to make it
eventually work for all arrays. The page
http://d-programming-language.org/template says to use

template TFoo(T : T[]) { ... } // #2


but when I try it, it doesn't quite work:

template abc(T)
  {
    void abc(T parm1)
      {
        writeln("simpleparm: ", parm1);
      }
  }

template abc(T: T[])
  {
    void abc(T parm1)
      {
        writeln("array : ", parm1);
      }
  }


void main(string[] args)
  {
    abc(1);
    abc("str");
    int[] arr = [1, 2];
    abc(arr);
  }


The output is:

simpleparm: 1
simpleparm: str
simpleparm: [1, 2]


Which is not what I want, it needs to be the specialized  template for
arrays. Note, this doesn't work either:

template abc(T: T[])
  {
    void abc(T[] parm1)
      {
        writeln("array : ", parm1);
      }
  }


John

--bcaec52161cf14621a04b0719a01
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

I have a template that I&#39;d like to have a specialization for arrays. In=
itiall I need it to work for byte arrays, but I&#39;d like to make it event=
ually work for all arrays. The page=A0<a href=3D"http://d-programming-langu=
age.org/template">http://d-programming-language.org/template</a>=A0says to =
use<div>
<br></div><blockquote class=3D"webkit-indent-blockquote" style=3D"margin: 0=
 0 0 40px; border: none; padding: 0px;"><div><font class=3D"Apple-style-spa=
n" face=3D"arial, helvetica, sans-serif"><span class=3D"Apple-style-span" s=
tyle=3D"font-size: 16px; background-color: rgb(255, 255, 255);"><span class=
=3D"d_keyword">template</span></span></font>=A0TFoo(T : T[]) { ... }=A0<spa=
n class=3D"d_comment">// #2</span></div>
</blockquote><div><div><br></div><div>but when I try it, it doesn&#39;t qui=
te work:</div><div><br></div><blockquote class=3D"webkit-indent-blockquote"=
 style=3D"margin: 0 0 0 40px; border: none; padding: 0px;"><div><div><font =
class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">templa=
te abc(T)</font></div>
</div><div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#=
39;, monospace">=A0 {</font></div></div><div><div><font class=3D"Apple-styl=
e-span" face=3D"&#39;courier new&#39;, monospace">=A0 =A0 void abc(T parm1)=
</font></div>
</div><div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#=
39;, monospace">=A0 =A0 =A0 {</font></div></div><div><div><font class=3D"Ap=
ple-style-span" face=3D"&#39;courier new&#39;, monospace">=A0 =A0 =A0 =A0 w=
riteln(&quot;simpleparm: &quot;, parm1);</font></div>
</div><div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#=
39;, monospace">=A0 =A0 =A0 }</font></div></div><div><div><font class=3D"Ap=
ple-style-span" face=3D"&#39;courier new&#39;, monospace">=A0 }</font></div=
</div><div>

ace"><br></font></div></div><div><div><font class=3D"Apple-style-span" face= =3D"&#39;courier new&#39;, monospace">template abc(T: T[])</font></div></di= v><div> <div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monosp= ace">=A0 {</font></div></div><div><div><font class=3D"Apple-style-span" fac= e=3D"&#39;courier new&#39;, monospace">=A0 =A0 void abc(T parm1)</font></di= v></div><div> <div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monosp= ace">=A0 =A0 =A0 {</font></div></div><div><div><font class=3D"Apple-style-s= pan" face=3D"&#39;courier new&#39;, monospace">=A0 =A0 =A0 =A0 writeln(&quo= t;array : &quot;, parm1);</font></div> </div><div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#= 39;, monospace">=A0 =A0 =A0 }</font></div></div><div><div><font class=3D"Ap= ple-style-span" face=3D"&#39;courier new&#39;, monospace">=A0 }</font></div=
</div><div>

ace"><br></font></div></div><div><div><font class=3D"Apple-style-span" face= =3D"&#39;courier new&#39;, monospace"><br></font></div></div><div><div><fon= t class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">void= main(string[] args)</font></div> </div><div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#= 39;, monospace">=A0 {</font></div></div><div><div><font class=3D"Apple-styl= e-span" face=3D"&#39;courier new&#39;, monospace">=A0 =A0 abc(1);</font></d= iv></div><div> <div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monosp= ace">=A0 =A0 abc(&quot;str&quot;);</font></div></div><div><div><font class= =3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">=A0 =A0 int= [] arr =3D [1, 2];</font></div> </div><div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#= 39;, monospace">=A0 =A0 abc(arr);</font></div></div><div><div><font class= =3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">=A0 } =A0</= font></div></div> </blockquote><div><div><br></div><div><div>The output is:</div></div></div>= <div><br></div><blockquote class=3D"webkit-indent-blockquote" style=3D"marg= in: 0 0 0 40px; border: none; padding: 0px;"><div><div><font class=3D"Apple= -style-span" face=3D"&#39;courier new&#39;, monospace">simpleparm: 1</font>= </div> </div><div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#= 39;, monospace">simpleparm: str</font></div></div><div><div><font class=3D"= Apple-style-span" face=3D"&#39;courier new&#39;, monospace">simpleparm: [1,= 2]</font></div> </div></blockquote><div><br></div><div>Which is not what I want, it needs t= o be the specialized =A0template for arrays. Note, this doesn&#39;t work ei= ther:</div><div><br></div><blockquote class=3D"webkit-indent-blockquote" st= yle=3D"margin: 0 0 0 40px; border: none; padding: 0px;"> <div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, m= onospace">template abc(T: T[])</font></div></div><div><div><font class=3D"A= pple-style-span" face=3D"&#39;courier new&#39;, monospace">=A0 {</font></di= v></div> <div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, m= onospace">=A0 =A0 void abc(T[] parm1)</font></div></div><div><div><font cla= ss=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">=A0 =A0 = =A0 {</font></div> </div><div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#= 39;, monospace">=A0 =A0 =A0 =A0 writeln(&quot;array : &quot;, parm1);</font=
</div></div><div><div><font class=3D"Apple-style-span" face=3D"&#39;courie=

</div><div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#= 39;, monospace">=A0 }</font></div></div></blockquote><div><br></div><div>Jo= hn</div></div> --bcaec52161cf14621a04b0719a01--
Oct 29 2011
next sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 10/29/2011 05:24 PM, J Arrizza wrote:
 I have a template that I'd like to have a specialization for arrays.
 Initiall I need it to work for byte arrays, but I'd like to make it
 eventually work for all arrays. The page
 http://d-programming-language.org/template says to use

     template TFoo(T : T[]) { ... } // #2


 but when I try it, it doesn't quite work:

     template abc(T)
        {
          void abc(T parm1)
            {
              writeln("simpleparm: ", parm1);
            }
        }

     template abc(T: T[])
        {
          void abc(T parm1)
            {
              writeln("array : ", parm1);
            }
        }


     void main(string[] args)
        {
          abc(1);
          abc("str");
          int[] arr = [1, 2];
          abc(arr);
        }


 The output is:

     simpleparm: 1
     simpleparm: str
     simpleparm: [1, 2]


 Which is not what I want, it needs to be the specialized  template for
 arrays. Note, this doesn't work either:

     template abc(T: T[])
        {
          void abc(T[] parm1)
            {
              writeln("array : ", parm1);
            }
        }


 John

Fixed: template abc(T) { void abc(T parm1) { writeln("simpleparm: ", parm1); } } void abc(T:T[])(T[] parm1) { writeln("array : ", parm1); } void main(string[] args) { abc(1); abc!(typeof("str"))("str"); int[] arr = [1, 2]; abc!(int[])(arr); } The important thing to note is that when pattern matching on T[] is done, then T is the element type of the array, not the array type.
Oct 29 2011
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 10/29/2011 11:32 PM, Timon Gehr wrote:
 On 10/29/2011 05:24 PM, J Arrizza wrote:
 I have a template that I'd like to have a specialization for arrays.
 Initiall I need it to work for byte arrays, but I'd like to make it
 eventually work for all arrays. The page
 http://d-programming-language.org/template says to use

 template TFoo(T : T[]) { ... } // #2


 but when I try it, it doesn't quite work:

 template abc(T)
 {
 void abc(T parm1)
 {
 writeln("simpleparm: ", parm1);
 }
 }

 template abc(T: T[])
 {
 void abc(T parm1)
 {
 writeln("array : ", parm1);
 }
 }


 void main(string[] args)
 {
 abc(1);
 abc("str");
 int[] arr = [1, 2];
 abc(arr);
 }


 The output is:

 simpleparm: 1
 simpleparm: str
 simpleparm: [1, 2]


 Which is not what I want, it needs to be the specialized template for
 arrays. Note, this doesn't work either:

 template abc(T: T[])
 {
 void abc(T[] parm1)
 {
 writeln("array : ", parm1);
 }
 }


 John

Fixed: template abc(T) { void abc(T parm1) { writeln("simpleparm: ", parm1); } } void abc(T:T[])(T[] parm1) { writeln("array : ", parm1); } void main(string[] args) { abc(1); abc!(typeof("str"))("str"); int[] arr = [1, 2]; abc!(int[])(arr); } The important thing to note is that when pattern matching on T[] is done, then T is the element type of the array, not the array type.

When using pattern matching, explicit template arguments are necessary. You probably don't want that, the following code does not need them: void abc(T)(T parm1) if(!isDynamicArray!T) { writeln("simpleparm: ", parm1); } void abc(T)(T parm1) if(isDynamicArray!T){ writeln("array : ", parm1); } void main(string[] args) { abc(1); abc("str"); int[] arr = [1, 2]; abc(arr); }
Oct 29 2011
next sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
This works too:

void abc(T, U=void)(T parm1) {
     writeln("simpleparm: ", parm1);
}

void abc(T:U[],U)(T parm1) {
     writeln("array : ", parm1);
}


void main(string[] args) {
     abc(1);
     abc("str");
     int[] arr = [1, 2];
     abc(arr);
}
Oct 29 2011
prev sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 10/30/2011 12:52 AM, J Arrizza wrote:
     When using pattern matching, explicit template arguments are
     necessary. You probably don't want that, the following code does not
     need them:

     void abc(T)(T parm1) if(!isDynamicArray!T)
     {
         writeln("simpleparm: ", parm1);
     }

     void abc(T)(T parm1) if(isDynamicArray!T){

         writeln("array : ", parm1);
     }


 The output is:

     simpleparm: 1
     dynamic array : str
     dynamic array : [1, 2]


 which sort of makes sense, but doesn't fit my app. It is possible to
 treat a string as an array of characters, but in my case I  want to
 treat them as a single entity. The whole purpose of the array
 specialization is to search/manipulate/compare the individual elements
 of an array... except for strings.

 Ok, so I modified a little to take care of strings. But I also added
 another test for a static array and it's not playing nice anymore.

     void abc(T, U=void) (T parm1)
        {
        writeln("simpleparm: ", parm1);
        }

     void abc(T: string) (T parm1)
        {
        writeln("string : ", parm1);
        }

     void abc(T:U[], U) (T parm1)
        if (isDynamicArray!T)
        {
        writeln("dynamic array : ", parm1);
        }

     void abc(T:U[], U) (T parm1)
        if (!isDynamicArray!T)  //tried isStaticArray as well here
        {
        writeln("static array : ", parm1);
        }

     void main(string[] args)
        {
          writeln("v4");
          abc(1);
          abc("str");
          int[] arr = [1, 2];
          abc(arr);
          int[2] arr2 =[3, 4];
          writeln("arr2 ", __traits(isStaticArray, arr2));
          abc(arr2);
        }


 Output is:

     simpleparm: 1
     string : str
     dynamic array : [1, 2]
     arr2 true
     simpleparm: [3, 4]      // should be "static array: [3, 4]"


 John

This works: void abc(T, U=void, size_t N=0) (T parm1) { writeln("simpleparm: ", parm1); } void abc(T: string) (T parm1) { writeln("string : ", parm1); } void abc(T:U[], U) (T parm1) if (isDynamicArray!T) { writeln("dynamic array : ", parm1); } void abc(T:U[N], U, size_t N) (T parm1) { writeln("static array : ", parm1); } void main(string[] args) { abc(1); abc("str"); int[] arr = [1, 2]; abc(arr); int[2] arr2 =[3, 4]; abc(arr2); }
Oct 29 2011
next sibling parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 10/29/11 6:02 PM, Timon Gehr wrote:
 This works:

 void abc(T, U=void, size_t N=0) (T parm1) {
 writeln("simpleparm: ", parm1);
 }

 void abc(T: string) (T parm1) {
 writeln("string : ", parm1);
 }

 void abc(T:U[], U) (T parm1) if (isDynamicArray!T) {
 writeln("dynamic array : ", parm1);
 }

 void abc(T:U[N], U, size_t N) (T parm1) {
 writeln("static array : ", parm1);
 }

 void main(string[] args) {
 abc(1);
 abc("str");
 int[] arr = [1, 2];
 abc(arr);
 int[2] arr2 =[3, 4];
 abc(arr2);
 }

What's wrong with isStaticArray? Also, OP may want to look at isNarrowString. Andrei
Oct 29 2011
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 10/29/11 6:44 PM, J Arrizza wrote:
 On Sat, Oct 29, 2011 at 4:14 PM, Andrei Alexandrescu
 <SeeWebsiteForEmail erdani.org <mailto:SeeWebsiteForEmail erdani.org>>
 wrote:


     What's wrong with isStaticArray? Also, OP may want to look at
     isNarrowString.


     Andrei


 Tried isStaticArray:

I think you got quite a bit of advice for different approaches, which may get confusing. You should use either std.traits.isXxx systematically, or patterns systematically, but not both at the same time. Personally I prefer isXxx because they foster simple logic to decide what overloads should apply. Also, when posting, you may want to include complete short programs so others can try them quickly. Andrei
Oct 29 2011
parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 10/30/11 5:16 AM, J Arrizza wrote:
         You should use either std.traits.isXxx systematically, or
         patterns systematically, but not both at the same time.
         Personally I prefer isXxx because they foster simple logic to
         decide what overloads should apply.


     Also, when posting, you may want to include complete short programs
     so others can try them quickly.


 Andrei, I thought I had posted the entire program. Here it is again
 using only traits as you recommend:

     import std.stdio;
     import std.traits;
     void abc(T) (T parm1)
        if (isNarrowString!T || (!isStaticArray!T && !isDynamicArray!T))
        {
        writeln("simpleparm: ", parm1);
        }
     void abc(T) (T parm1)
        if (!isNarrowString!T && (isDynamicArray!T || isStaticArray!T) )
        {
        writeln("array : ", parm1);
        }
     void main(string[] args)
        {
          writeln("v4");
          abc(1);
          abc("str");
          int[] arr = [1, 2];
          abc(arr);
          int[2] arr2 = [3, 4];
          abc(arr2);
        }

Thanks, sorry for having missed that. The code as above is canonical. I think restricted templates are the way to go for most code. Pattern matching on types is rather arcane and should be let to a few advanced uses (such as implementing traits themselves). Andrei
Oct 30 2011
prev sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 10/30/2011 01:34 AM, J Arrizza wrote:
 Thank you Timon,  I very much appreciate your (and others) help.

 I will look all this up in the online docs and the book.

 There is still some very odd things in how these fit together. For
 example, if I comment out the last 3 function templates, I get all
 "simpleparms", which is expected.

 But if I add in the fourth, I get a compilation error saying the call to
 abc(arr2) is ambiguous. So even though the first one specifies "U =
 void" and "size_t N = 0", and the fourth specifies something very
 different, there is ambiguity between them.

 Adding in the third resolves the ambiguity between the first and fourth.
 John

I cannot reproduce that behaviour. If I comment out the second and the third, I get 3 "simpleparm"s and 1 "static array"
 On Sat, Oct 29, 2011 at 4:02 PM, Timon Gehr <timon.gehr gmx.ch
 <mailto:timon.gehr gmx.ch>> wrote:


     This works:

     void abc(T, U=void, size_t N=0) (T parm1) {
         writeln("simpleparm: ", parm1);
     }

     void abc(T: string) (T parm1) {
         writeln("string : ", parm1);
     }

     void abc(T:U[], U) (T parm1) if (isDynamicArray!T) {
         writeln("dynamic array : ", parm1);
       }

     void abc(T:U[N], U, size_t N) (T parm1) {
         writeln("static array : ", parm1);

     }

     void main(string[] args) {
         abc(1);
         abc("str");
         int[] arr = [1, 2];
         abc(arr);
         int[2] arr2 =[3, 4];
         abc(arr2);
     }

Whoops, just noticed that I accidentally left the if(isDynamicArray!T) in there. That is not necessary, if the pattern matches it will always evaluate to true. void abc(T, U=void, size_t N=0) (T parm1) { writeln("simpleparm: ", parm1); } //void abc(T: string) (T parm1) { // writeln("string : ", parm1); //} //void abc(T:U[], U) (T parm1){ // no constraint necessary // writeln("dynamic array : ", parm1); //} void abc(T:U[N], U, size_t N) (T parm1) { writeln("static array : ", parm1); } void main(string[] args) { abc(1); // simpleparm abc("str"); // simpleparm int[] arr = [1, 2]; abc(arr); // simpleparm int[2] arr2 =[3, 4]; abc(arr2); // static array } What compiler version are you using?
Oct 30 2011
prev sibling next sibling parent J Arrizza <cppgent0 gmail.com> writes:
--f46d041037999c0a5104b077dd01
Content-Type: text/plain; charset=ISO-8859-1

 When using pattern matching, explicit template arguments are necessary.
 You probably don't want that, the following code does not need them:

 void abc(T)(T parm1) if(!isDynamicArray!T)
 {
    writeln("simpleparm: ", parm1);
 }

 void abc(T)(T parm1) if(isDynamicArray!T){

    writeln("array : ", parm1);
 }

The output is: simpleparm: 1 dynamic array : str dynamic array : [1, 2] which sort of makes sense, but doesn't fit my app. It is possible to treat a string as an array of characters, but in my case I want to treat them as a single entity. The whole purpose of the array specialization is to search/manipulate/compare the individual elements of an array... except for strings. Ok, so I modified a little to take care of strings. But I also added another test for a static array and it's not playing nice anymore. void abc(T, U=void) (T parm1) { writeln("simpleparm: ", parm1); } void abc(T: string) (T parm1) { writeln("string : ", parm1); } void abc(T:U[], U) (T parm1) if (isDynamicArray!T) { writeln("dynamic array : ", parm1); } void abc(T:U[], U) (T parm1) if (!isDynamicArray!T) //tried isStaticArray as well here { writeln("static array : ", parm1); } void main(string[] args) { writeln("v4"); abc(1); abc("str"); int[] arr = [1, 2]; abc(arr); int[2] arr2 =[3, 4]; writeln("arr2 ", __traits(isStaticArray, arr2)); abc(arr2); } Output is: simpleparm: 1 string : str dynamic array : [1, 2] arr2 true simpleparm: [3, 4] // should be "static array: [3, 4]" John --f46d041037999c0a5104b077dd01 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote"><blockquote class=3D"gmail_quote" style=3D"margi= n:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">When using patte= rn matching, explicit template arguments are necessary. You probably don&#3= 9;t want that, the following code does not need them:<br> <br> void abc(T)(T parm1) if(!isDynamicArray!T)<br> {<br> =A0 =A0writeln(&quot;simpleparm: &quot;, parm1);<br> }<br> <br> void abc(T)(T parm1) if(isDynamicArray!T){<div><div></div><div class=3D"h5"=
<br>

}<br></div></div></blockquote><div><br></div><div><div>The output is:</div>= <div><br></div><blockquote class=3D"webkit-indent-blockquote" style=3D"marg= in-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 40px; bord= er-top-style: none; border-right-style: none; border-bottom-style: none; bo= rder-left-style: none; border-width: initial; border-color: initial; paddin= g-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "> <div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monosp= ace">simpleparm: 1</font></div><div><font class=3D"Apple-style-span" face= =3D"&#39;courier new&#39;, monospace">dynamic array : str</font></div><div>= <font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">= dynamic array : [1, 2]</font></div> </blockquote><div><br></div><div>which sort of makes sense, but doesn&#39;t= fit my app. It is possible to treat a string as an array of characters, bu= t in my case I =A0want to treat them as a single entity. The whole purpose = of the array specialization is to search/manipulate/compare the individual = elements of an array... except for strings.</div> </div><div><br></div><div>Ok, so I modified a little to take care of string= s. But I also added another test for a static array and it&#39;s not playin= g nice anymore.=A0</div><div><br></div></div><blockquote class=3D"webkit-in= dent-blockquote" style=3D"margin: 0 0 0 40px; border: none; padding: 0px;"> <div class=3D"gmail_quote"><div><div><font class=3D"Apple-style-span" face= =3D"&#39;courier new&#39;, monospace">void abc(T, U=3Dvoid) (T parm1)</font=
</div></div></div><div class=3D"gmail_quote"><div><div><font class=3D"Appl=

</div></div><div class=3D"gmail_quote"><div><div><font class=3D"Apple-style= -span" face=3D"&#39;courier new&#39;, monospace">=A0 writeln(&quot;simplepa= rm: &quot;, parm1);</font></div></div></div><div class=3D"gmail_quote"><div=
<div>

=A0 }</font></div></div></div><div class=3D"gmail_quote"><div><div><font cl= ass=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace"><br></fo= nt></div> </div></div><div class=3D"gmail_quote"><div><div><font class=3D"Apple-style= -span" face=3D"&#39;courier new&#39;, monospace">void abc(T: string) (T par= m1)</font></div></div></div><div class=3D"gmail_quote"><div><div><font clas= s=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">=A0 {</fon= t></div> </div></div><div class=3D"gmail_quote"><div><div><font class=3D"Apple-style= -span" face=3D"&#39;courier new&#39;, monospace">=A0 writeln(&quot;string := &quot;, parm1);</font></div></div></div><div class=3D"gmail_quote"><div><d= iv><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospac= e">=A0 }</font></div> </div></div><div class=3D"gmail_quote"><div><div><font class=3D"Apple-style= -span" face=3D"&#39;courier new&#39;, monospace"><br></font></div></div></d= iv><div class=3D"gmail_quote"><div><div><font class=3D"Apple-style-span" fa= ce=3D"&#39;courier new&#39;, monospace">void abc(T:U[], U) (T parm1)</font>= </div> </div></div><div class=3D"gmail_quote"><div><div><font class=3D"Apple-style= -span" face=3D"&#39;courier new&#39;, monospace">=A0 if (isDynamicArray!T)<= /font></div></div></div><div class=3D"gmail_quote"><div><div><font class=3D= "Apple-style-span" face=3D"&#39;courier new&#39;, monospace">=A0 {</font></= div> </div></div><div class=3D"gmail_quote"><div><div><font class=3D"Apple-style= -span" face=3D"&#39;courier new&#39;, monospace">=A0 writeln(&quot;dynamic = array : &quot;, parm1);</font></div></div></div><div class=3D"gmail_quote">= <div><div> <font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">= =A0 }</font></div></div></div><div class=3D"gmail_quote"><div><div><font cl= ass=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace"><br></fo= nt></div> </div></div><div class=3D"gmail_quote"><div><div><font class=3D"Apple-style= -span" face=3D"&#39;courier new&#39;, monospace">void abc(T:U[], U) (T parm= 1)</font></div></div></div><div class=3D"gmail_quote"><div><div><font class= =3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">=A0 if (!is= DynamicArray!T) =A0//tried isStaticArray as well here</font></div> </div></div><div class=3D"gmail_quote"><div><div><font class=3D"Apple-style= -span" face=3D"&#39;courier new&#39;, monospace">=A0 {</font></div></div></= div><div class=3D"gmail_quote"><div><div><font class=3D"Apple-style-span" f= ace=3D"&#39;courier new&#39;, monospace">=A0 writeln(&quot;static array : &= quot;, parm1);</font></div> </div></div><div class=3D"gmail_quote"><div><div><font class=3D"Apple-style= -span" face=3D"&#39;courier new&#39;, monospace">=A0 }</font></div></div></= div><div class=3D"gmail_quote"><div><div><font class=3D"Apple-style-span" f= ace=3D"&#39;courier new&#39;, monospace"><br> </font></div></div></div><div class=3D"gmail_quote"><div><div><font class= =3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">void main(s= tring[] args)</font></div></div></div><div class=3D"gmail_quote"><div><div>= <font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">= =A0 {</font></div> </div></div><div class=3D"gmail_quote"><div><div><font class=3D"Apple-style= -span" face=3D"&#39;courier new&#39;, monospace">=A0 =A0 writeln(&quot;v4&q= uot;);</font></div></div></div><div class=3D"gmail_quote"><div><div><font c= lass=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">=A0 =A0= abc(1);</font></div> </div></div><div class=3D"gmail_quote"><div><div><font class=3D"Apple-style= -span" face=3D"&#39;courier new&#39;, monospace">=A0 =A0 abc(&quot;str&quot= ;);</font></div></div></div><div class=3D"gmail_quote"><div><div><font clas= s=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">=A0 =A0 in= t[] arr =3D [1, 2];</font></div> </div></div><div class=3D"gmail_quote"><div><div><font class=3D"Apple-style= -span" face=3D"&#39;courier new&#39;, monospace">=A0 =A0 abc(arr);</font></= div></div></div><div class=3D"gmail_quote"><div><div><font class=3D"Apple-s= tyle-span" face=3D"&#39;courier new&#39;, monospace">=A0 =A0 int[2] arr2 = =3D[3, 4];<br> =A0 =A0 writeln(&quot;arr2 &quot;, __traits(isStaticArray, arr2));</font></= div></div></div><div class=3D"gmail_quote"><div><div><font class=3D"Apple-s= tyle-span" face=3D"&#39;courier new&#39;, monospace">=A0 =A0 abc(arr2);</fo= nt></div></div> </div><div class=3D"gmail_quote"><div><div><font class=3D"Apple-style-span"= face=3D"&#39;courier new&#39;, monospace">=A0 } =A0</font></div></div></di= v></blockquote><div class=3D"gmail_quote"><div><br></div><div>Output is:</d= iv></div> <blockquote class=3D"webkit-indent-blockquote" style=3D"margin: 0 0 0 40px;= border: none; padding: 0px;"><div class=3D"gmail_quote"><div><div><font cl= ass=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">simplepa= rm: 1</font></div> </div></div><div class=3D"gmail_quote"><div><div><font class=3D"Apple-style= -span" face=3D"&#39;courier new&#39;, monospace">string : str</font></div><= /div></div><div class=3D"gmail_quote"><div><div><font class=3D"Apple-style-= span" face=3D"&#39;courier new&#39;, monospace">dynamic array : [1, 2]</fon= t></div> </div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, = monospace">arr2 true</font></div></div><div class=3D"gmail_quote"><div><div=
<font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace"=
simpleparm: [3, 4] =A0 =A0 =A0// should be &quot;static array: [3, 4]&quot=

</div></div></blockquote><div class=3D"gmail_quote"><div><br></div><div>Joh= n</div><div><br></div><div><br></div></div><div><br></div> --f46d041037999c0a5104b077dd01--
Oct 29 2011
prev sibling next sibling parent J Arrizza <cppgent0 gmail.com> writes:
--f46d0410379980d9df04b07872b8
Content-Type: text/plain; charset=ISO-8859-1

Thank you Timon,  I very much appreciate your (and others) help.

I will look all this up in the online docs and the book.

There is still some very odd things in how these fit together. For example,
if I comment out the last 3 function templates, I get all "simpleparms",
which is expected.

But if I add in the fourth, I get a compilation error saying the call to
abc(arr2) is ambiguous. So even though the first one specifies "U = void"
and "size_t N = 0", and the fourth specifies something very different,
there is ambiguity between them.

Adding in the third resolves the ambiguity between the first and fourth.

John

On Sat, Oct 29, 2011 at 4:02 PM, Timon Gehr <timon.gehr gmx.ch> wrote:

 This works:

 void abc(T, U=void, size_t N=0) (T parm1) {
    writeln("simpleparm: ", parm1);
 }

 void abc(T: string) (T parm1) {
    writeln("string : ", parm1);
 }

 void abc(T:U[], U) (T parm1) if (isDynamicArray!T) {
    writeln("dynamic array : ", parm1);
  }

 void abc(T:U[N], U, size_t N) (T parm1) {
    writeln("static array : ", parm1);

 }

 void main(string[] args) {
    abc(1);
    abc("str");
    int[] arr = [1, 2];
    abc(arr);
    int[2] arr2 =[3, 4];
    abc(arr2);
 }

--f46d0410379980d9df04b07872b8 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Thank you Timon, =A0I very much appreciate your (and others) help.<div><br>= </div><div>I will look all this up in the online docs and the book.=A0</div=
<div><br></div><div>There is still some very odd things in how these fit t=

et all &quot;simpleparms&quot;, which is expected.=A0</div> <div><br></div><div>But if I add in the fourth, I get a compilation error s= aying the call to abc(arr2) is ambiguous. So even though the first one spec= ifies &quot;U =3D void&quot; and &quot;size_t N =3D 0&quot;, and the fourth= specifies something very different, there is ambiguity between them.=A0</d= iv> <div><br></div><div>Adding in the third resolves the ambiguity between the = first and fourth.</div><div>=A0</div><div>John</div><div><br><div class=3D"= gmail_quote">On Sat, Oct 29, 2011 at 4:02 PM, Timon Gehr <span dir=3D"ltr">= &lt;<a href=3D"mailto:timon.gehr gmx.ch">timon.gehr gmx.ch</a>&gt;</span> w= rote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex;"><div class=3D"im"><br></div> This works:<br> <br> void abc(T, U=3Dvoid, size_t N=3D0) (T parm1) {<br> =A0 =A0writeln(&quot;simpleparm: &quot;, parm1);<br> }<br><div class=3D"im"> <br> void abc(T: string) (T parm1) {<br> =A0 =A0writeln(&quot;string : &quot;, parm1);<br> }<br> <br> void abc(T:U[], U) (T parm1) if (isDynamicArray!T) {<br> =A0 =A0writeln(&quot;dynamic array : &quot;, parm1);<br> =A0}<br> <br></div> void abc(T:U[N], U, size_t N) (T parm1) {<br> =A0 =A0writeln(&quot;static array : &quot;, parm1);<div class=3D"im"><br> }<br> <br> void main(string[] args) {<br> =A0 =A0abc(1);<br> =A0 =A0abc(&quot;str&quot;);<br> =A0 =A0int[] arr =3D [1, 2];<br> =A0 =A0abc(arr);<br></div><div class=3D"im"> =A0 =A0int[2] arr2 =3D[3, 4];<br></div> =A0 =A0abc(arr2);<br> }<br> </blockquote></div><br> </div> --f46d0410379980d9df04b07872b8--
Oct 29 2011
prev sibling next sibling parent J Arrizza <cppgent0 gmail.com> writes:
--bcaec52160bb6f231d04b078952a
Content-Type: text/plain; charset=ISO-8859-1

On Sat, Oct 29, 2011 at 4:14 PM, Andrei Alexandrescu <
SeeWebsiteForEmail erdani.org> wrote:

 What's wrong with isStaticArray? Also, OP may want to look at
 isNarrowString.


 Andrei

Tried isStaticArray: void abc(T:U[], U) (T parm1) if (isDynamicArray!T) { writeln("dynamic array : ", parm1); } void abc(T:U[], U) (T parm1) if (isStaticArray!T) { writeln("static array : ", parm1); } It didn't match. The output was: simpleparm: 1 dynamic array : str dynamic array : [1, 2] simpleparm: [3, 4] isNarrowString isn't in the traits online doc. Looked it up in std/traits.d and I tried it: void abc(T) (T parm1) if (isNarrowString!T) { writeln("string : ", parm1); } void abc(T:U[], U) (T parm1) if (isDynamicArray!T) { writeln("dynamic array : ", parm1); } and get compiler ambiguity for abc("str") between the two templates above. John --bcaec52160bb6f231d04b078952a Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">On Sat, Oct 29, 2011 at 4:14 PM, Andrei Alexandr= escu <span dir=3D"ltr">&lt;<a href=3D"mailto:SeeWebsiteForEmail erdani.org"=
SeeWebsiteForEmail erdani.org</a>&gt;</span> wrote:<br><blockquote class=

ing-left:1ex;"> <div class=3D"im"><br></div> What&#39;s wrong with isStaticArray? Also, OP may want to look at isNarrowS= tring.<br><font color=3D"#888888"> <br> <br> Andrei<br> </font></blockquote></div><br>Tried isStaticArray: <blockquote class=3D"webkit-indent-blockquote" style=3D"margin: 0 0 0 40px;= border: none; padding: 0px;"><div><div><font class=3D"Apple-style-span" fa= ce=3D"&#39;courier new&#39;, monospace">void abc(T:U[], U) (T parm1)</font>= </div> </div><div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#= 39;, monospace">=A0 if (isDynamicArray!T)</font></div></div><div><div><font= class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">=A0 {= </font></div> </div><div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#= 39;, monospace">=A0 writeln(&quot;dynamic array : &quot;, parm1);</font></d= iv></div><div><div><font class=3D"Apple-style-span" face=3D"&#39;courier ne= w&#39;, monospace">=A0 }</font></div> </div><div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#= 39;, monospace">void abc(T:U[], U) (T parm1)</font></div></div><div><div><f= ont class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">= =A0 if (isStaticArray!T)</font></div> </div><div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#= 39;, monospace">=A0 {</font></div></div><div><div><font class=3D"Apple-styl= e-span" face=3D"&#39;courier new&#39;, monospace">=A0 writeln(&quot;static = array : &quot;, parm1);</font></div> </div><div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#= 39;, monospace">=A0 }</font></div></div></blockquote><div><br></div><div>It= didn&#39;t match. The output was:</div><blockquote class=3D"webkit-indent-= blockquote" style=3D"margin: 0 0 0 40px; border: none; padding: 0px;"> <div><div style=3D"text-align: left;"><font class=3D"Apple-style-span" face= =3D"&#39;courier new&#39;, monospace">simpleparm: 1</font></div></div><div>= <div style=3D"text-align: left;"><font class=3D"Apple-style-span" face=3D"&= #39;courier new&#39;, monospace">dynamic array : str</font></div> </div><div><div style=3D"text-align: left;"><font class=3D"Apple-style-span= " face=3D"&#39;courier new&#39;, monospace">dynamic array : [1, 2]</font></= div></div><div><div style=3D"text-align: left;"><font class=3D"Apple-style-= span" face=3D"&#39;courier new&#39;, monospace">simpleparm: [3, 4]</font></= div> </div></blockquote><div style=3D"text-align: left;"><br></div><div style=3D= "text-align: left;">isNarrowString isn&#39;t in the traits online doc. Look= ed it up in std/traits.d and I tried it:</div><div style=3D"text-align: lef= t;"> <br></div><blockquote class=3D"webkit-indent-blockquote" style=3D"margin: 0= 0 0 40px; border: none; padding: 0px;"><div style=3D"text-align: left;"><d= iv style=3D"text-align: left;"><font class=3D"Apple-style-span" face=3D"&#3= 9;courier new&#39;, monospace">void abc(T) (T parm1)</font></div> </div><div style=3D"text-align: left;"><div style=3D"text-align: left;"><fo= nt class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">=A0= if (isNarrowString!T)</font></div></div><div style=3D"text-align: left;"><= div style=3D"text-align: left;"> <font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">= =A0 {</font></div></div><div style=3D"text-align: left;"><div style=3D"text= -align: left;"><font class=3D"Apple-style-span" face=3D"&#39;courier new&#3= 9;, monospace">=A0 writeln(&quot;string : &quot;, parm1);</font></div> </div><div style=3D"text-align: left;"><div style=3D"text-align: left;"><fo= nt class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">=A0= }</font></div></div><div style=3D"text-align: left;"><div style=3D"text-al= ign: left;"> <font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">= void abc(T:U[], U) (T parm1)</font></div></div><div style=3D"text-align: le= ft;"><div style=3D"text-align: left;"><font class=3D"Apple-style-span" face= =3D"&#39;courier new&#39;, monospace">=A0 if (isDynamicArray!T)</font></div=

nt class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">=A0= {</font></div></div><div style=3D"text-align: left;"><div style=3D"text-al= ign: left;"> <font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">= =A0 writeln(&quot;dynamic array : &quot;, parm1);</font></div></div><div st= yle=3D"text-align: left;"><div style=3D"text-align: left;"><font class=3D"A= pple-style-span" face=3D"&#39;courier new&#39;, monospace">=A0 }</font></di= v> </div></blockquote><div style=3D"text-align: left;"><div style=3D"text-alig= n: left;"><br></div><div style=3D"text-align: left;">and get compiler ambig= uity for abc(&quot;str&quot;) between the two templates above.</div><div st= yle=3D"text-align: left;"> <br></div><div style=3D"text-align: left;">John</div></div> --bcaec52160bb6f231d04b078952a--
Oct 29 2011
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Saturday, October 29, 2011 16:44:16 J Arrizza wrote:
 On Sat, Oct 29, 2011 at 4:14 PM, Andrei Alexandrescu <
 
 SeeWebsiteForEmail erdani.org> wrote:
 What's wrong with isStaticArray? Also, OP may want to look at
 isNarrowString.
 
 
 Andrei

Tried isStaticArray: void abc(T:U[], U) (T parm1) if (isDynamicArray!T) { writeln("dynamic array : ", parm1); } void abc(T:U[], U) (T parm1) if (isStaticArray!T) { writeln("static array : ", parm1); } It didn't match. The output was: simpleparm: 1 dynamic array : str dynamic array : [1, 2] simpleparm: [3, 4] isNarrowString isn't in the traits online doc. Looked it up in std/traits.d and I tried it: void abc(T) (T parm1) if (isNarrowString!T) { writeln("string : ", parm1); } void abc(T:U[], U) (T parm1) if (isDynamicArray!T) { writeln("dynamic array : ", parm1); } and get compiler ambiguity for abc("str") between the two templates above.

Of course you do. A narrow string is a dynamic array, so it matches both. You need to change the second constraint to if(isDynamicArray!T && !isNarrowString!T), then narrow strings won't match both. - Jonathan M Davis
Oct 29 2011
prev sibling next sibling parent J Arrizza <cppgent0 gmail.com> writes:
--bcaec5215f712d7ab404b0793d75
Content-Type: text/plain; charset=ISO-8859-1

On Sat, Oct 29, 2011 at 5:16 PM, Jonathan M Davis <jmdavisProg gmx.com>wrote:

 and get compiler ambiguity for abc("str") between the two templates

Of course you do. A narrow string is a dynamic array, so it matches both. You need to change the second constraint to if(isDynamicArray!T && !isNarrowString!T), then narrow strings won't match both. - Jonathan M Davis

Yup, just tried it and works fine. --bcaec5215f712d7ab404b0793d75 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <br><br><div class=3D"gmail_quote">On Sat, Oct 29, 2011 at 5:16 PM, Jonatha= n M Davis <span dir=3D"ltr">&lt;<a href=3D"mailto:jmdavisProg gmx.com">jmda= visProg gmx.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" = style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"> <div><div></div><div class=3D"h5">&gt; and get compiler ambiguity for abc(&= quot;str&quot;) between the two templates above.<br> <br> </div></div>Of course you do. A narrow string is a dynamic array, so it mat= ches both. You<br> need to change the second constraint to if(isDynamicArray!T &amp;&amp;<br> !isNarrowString!T), then narrow strings won&#39;t match both.<br> <font color=3D"#888888"><br> - Jonathan M Davis<br> </font></blockquote></div><div><br></div>Yup, just tried it and works fine.= <br><br> --bcaec5215f712d7ab404b0793d75--
Oct 29 2011
prev sibling next sibling parent J Arrizza <cppgent0 gmail.com> writes:
--bcaec51dd49144dc6204b0816bf6
Content-Type: text/plain; charset=ISO-8859-1

 You should use either std.traits.isXxx systematically, or patterns
 systematically, but not both at the same time. Personally I prefer isXxx
 because they foster simple logic to decide what overloads should apply.

Also, when posting, you may want to include complete short programs so others can try them quickly.

Andrei, I thought I had posted the entire program. Here it is again using only traits as you recommend: import std.stdio; import std.traits; void abc(T) (T parm1) if (isNarrowString!T || (!isStaticArray!T && !isDynamicArray!T)) { writeln("simpleparm: ", parm1); } void abc(T) (T parm1) if (!isNarrowString!T && (isDynamicArray!T || isStaticArray!T) ) { writeln("array : ", parm1); } void main(string[] args) { writeln("v4"); abc(1); abc("str"); int[] arr = [1, 2]; abc(arr); int[2] arr2 = [3, 4]; abc(arr2); } And it does work, here's the output: simpleparm: 1 simpleparm: str array : [1, 2] array : [3, 4] Note my original intent was to differentiate between arrays and non-arrays only (lumping strings into non-array). As for using only patterns, I can't get the compiler to disambiguate between non-arrays and arrays: import std.stdio; import std.traits; void abc(T, U = void, size_t N = 0) (T parm1) //line 3 { writeln("simpleparm: ", parm1); } void abc(T: U[N], U = char, size_t N) (T parm1) { writeln("string : ", parm1); } void abc(T) (T parm1) { writeln("dynamic array : ", parm1); } void abc(T: U[N], U, size_t N) (T parm1) //line 15 { writeln("static array : ", parm1); } void main(string[] args) { writeln("v4"); abc(1); abc("str"); int[] arr = [1, 2]; abc(arr); int[2] arr2 = [3, 4]; abc(arr2); //line 27 } Here's the compiler error: dtest.d(27): Error: template dtest.abc(T,U = void,ulong N = 0) abc(T,U = void,ulong N = 0) matches more than one template declaration, dtest.d(3):abc(T,U = void,ulong N = 0) and dtest.d(15):abc(T : U[N],U,ulong N) --bcaec51dd49144dc6204b0816bf6 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote"><blockquote class=3D"gmail_quote" style=3D"margi= n:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><blockquote clas= s=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;pad= ding-left:1ex"> You should use either std.traits.isXxx systematically, or patterns systemat= ically, but not both at the same time. Personally I prefer isXxx because th= ey foster simple logic to decide what overloads should apply.</blockquote> <br> Also, when posting, you may want to include complete short programs so othe= rs can try them quickly.</blockquote><div><br></div><div>Andrei, I thought = I had posted the entire program. Here it is again using only traits as you = recommend:</div> <div><br></div></div><blockquote class=3D"webkit-indent-blockquote" style= =3D"margin: 0 0 0 40px; border: none; padding: 0px;"><div class=3D"gmail_qu= ote"><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, m= onospace"><div> <div>import std.stdio;</div><div>import std.traits;</div><div>void abc(T) (= T parm1)</div><div>=A0 if (isNarrowString!T || (!isStaticArray!T &amp;&amp;= !isDynamicArray!T))</div><div>=A0 {</div><div>=A0 writeln(&quot;simpleparm= : &quot;, parm1);</div> <div>=A0 }</div><div>void abc(T) (T parm1)</div><div>=A0 if (!isNarrowStrin= g!T &amp;&amp; (isDynamicArray!T || isStaticArray!T) )</div><div>=A0 {</div=
<div>=A0 writeln(&quot;array : &quot;, parm1);</div><div>=A0 }</div><div>v=

<div>=A0 {</div><div>=A0 =A0 writeln(&quot;v4&quot;);</div><div>=A0 =A0 abc= (1);</div><div>=A0 =A0 abc(&quot;str&quot;);</div><div>=A0 =A0 int[] arr = =3D [1, 2];</div><div>=A0 =A0 abc(arr);</div><div>=A0 =A0 int[2] arr2 =3D [= 3, 4];</div><div>=A0 =A0 abc(arr2);</div> <div>=A0 }</div></div><div><br></div></font></div></div></blockquote><div c= lass=3D"gmail_quote"><div class=3D"gmail_quote"><div>And it does work, here= &#39;s the output:</div></div></div><blockquote class=3D"webkit-indent-bloc= kquote" style=3D"margin: 0 0 0 40px; border: none; padding: 0px;"> <div class=3D"gmail_quote"><div class=3D"gmail_quote"><div><font class=3D"A= pple-style-span" face=3D"&#39;courier new&#39;, monospace"><div>simpleparm:= 1</div><div>simpleparm: str</div><div>array : [1, 2]</div><div>array : [3,= 4]</div> </font></div></div></div></blockquote><div class=3D"gmail_quote"><div class= =3D"gmail_quote"><div><br></div><div>Note my original intent was to differe= ntiate between arrays and non-arrays only (lumping strings into non-array).= </div> <div><br></div><div>As for using only patterns, I can&#39;t get the compile= r to disambiguate between non-arrays and arrays:</div><div><br></div></div>= </div><blockquote class=3D"webkit-indent-blockquote" style=3D"margin: 0 0 0= 40px; border: none; padding: 0px;"> <div class=3D"gmail_quote"><div class=3D"gmail_quote"><div><div><font class= =3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">import std.= stdio;</font></div></div></div></div><div class=3D"gmail_quote"><div class= =3D"gmail_quote"> <div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, m= onospace">import std.traits;</font></div></div></div></div><div class=3D"gm= ail_quote"><div class=3D"gmail_quote"><div><div><font class=3D"Apple-style-= span" face=3D"&#39;courier new&#39;, monospace">void abc(T, U =3D void, siz= e_t N =3D 0) (T parm1) =A0 //line 3</font></div> </div></div></div><div class=3D"gmail_quote"><div class=3D"gmail_quote"><di= v><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, mono= space">=A0 {</font></div></div></div></div><div class=3D"gmail_quote"><div = class=3D"gmail_quote"> <div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, m= onospace">=A0 writeln(&quot;simpleparm: &quot;, parm1);</font></div></div><= /div></div><div class=3D"gmail_quote"><div class=3D"gmail_quote"><div><div>= <font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">= =A0 }</font></div> </div></div></div><div class=3D"gmail_quote"><div class=3D"gmail_quote"><di= v><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, mono= space">void abc(T: U[N], U =3D char, size_t N) (T parm1)</font></div></div>= </div> </div><div class=3D"gmail_quote"><div class=3D"gmail_quote"><div><div><font= class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">=A0 {= </font></div></div></div></div><div class=3D"gmail_quote"><div class=3D"gma= il_quote"> <div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, m= onospace">=A0 writeln(&quot;string : &quot;, parm1);</font></div></div></di= v></div><div class=3D"gmail_quote"><div class=3D"gmail_quote"><div><div><fo= nt class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">=A0= }</font></div> </div></div></div><div class=3D"gmail_quote"><div class=3D"gmail_quote"><di= v><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, mono= space">void abc(T) (T parm1)</font></div></div></div></div><div class=3D"gm= ail_quote"> <div class=3D"gmail_quote"><div><div><font class=3D"Apple-style-span" face= =3D"&#39;courier new&#39;, monospace">=A0 {</font></div></div></div></div><= div class=3D"gmail_quote"><div class=3D"gmail_quote"><div><div><font class= =3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">=A0 writeln= (&quot;dynamic array : &quot;, parm1);</font></div> </div></div></div><div class=3D"gmail_quote"><div class=3D"gmail_quote"><di= v><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, mono= space">=A0 }</font></div></div></div></div><div class=3D"gmail_quote"><div = class=3D"gmail_quote"> <div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, m= onospace">void abc(T: U[N], U, size_t N) (T parm1) =A0 =A0=A0</font><span c= lass=3D"Apple-style-span" style=3D"font-family: &#39;courier new&#39;, mono= space; ">//line 15</span></div> </div></div></div><div class=3D"gmail_quote"><div class=3D"gmail_quote"><di= v><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, mono= space">=A0 {</font></div></div></div></div><div class=3D"gmail_quote"><div = class=3D"gmail_quote"> <div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, m= onospace">=A0 writeln(&quot;static array : &quot;, parm1);</font></div></di= v></div></div><div class=3D"gmail_quote"><div class=3D"gmail_quote"><div><d= iv><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospac= e">=A0 }</font></div> </div></div></div><div class=3D"gmail_quote"><div class=3D"gmail_quote"><di= v><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, mono= space">void main(string[] args)</font></div></div></div></div><div class=3D= "gmail_quote"> <div class=3D"gmail_quote"><div><div><font class=3D"Apple-style-span" face= =3D"&#39;courier new&#39;, monospace">=A0 {</font></div></div></div></div><= div class=3D"gmail_quote"><div class=3D"gmail_quote"><div><div><font class= =3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">=A0 =A0 wri= teln(&quot;v4&quot;);</font></div> </div></div></div><div class=3D"gmail_quote"><div class=3D"gmail_quote"><di= v><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, mono= space">=A0 =A0 abc(1);</font></div></div></div></div><div class=3D"gmail_qu= ote"><div class=3D"gmail_quote"> <div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, m= onospace">=A0 =A0 abc(&quot;str&quot;);</font></div></div></div></div><div = class=3D"gmail_quote"><div class=3D"gmail_quote"><div><div><font class=3D"A= pple-style-span" face=3D"&#39;courier new&#39;, monospace">=A0 =A0 int[] ar= r =3D [1, 2];</font></div> </div></div></div><div class=3D"gmail_quote"><div class=3D"gmail_quote"><di= v><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, mono= space">=A0 =A0 abc(arr);</font></div></div></div></div><div class=3D"gmail_= quote"><div class=3D"gmail_quote"> <div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, m= onospace">=A0 =A0 int[2] arr2 =3D [3, 4];</font></div></div></div></div><di= v class=3D"gmail_quote"><div class=3D"gmail_quote"><div><div><font class=3D= "Apple-style-span" face=3D"&#39;courier new&#39;, monospace">=A0 =A0 abc(ar= r2); =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0//line 27</font></div> </div></div></div><div class=3D"gmail_quote"><div class=3D"gmail_quote"><di= v><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, mono= space">=A0 }</font></div></div></div></div></blockquote><div class=3D"gmail= _quote"> <div class=3D"gmail_quote"><div><br></div><div>Here&#39;s the compiler erro= r:</div><div><br></div></div></div><blockquote class=3D"webkit-indent-block= quote" style=3D"margin: 0 0 0 40px; border: none; padding: 0px;"><div class= =3D"gmail_quote"> <div class=3D"gmail_quote"><div><div><font class=3D"Apple-style-span" face= =3D"&#39;courier new&#39;, monospace">dtest.d(27): Error: template dtest.ab= c(T,U =3D void,ulong N =3D 0) abc(T,U =3D void,ulong N =3D 0) matches more = than one template declaration, dtest.d(3):abc(T,U =3D void,ulong N =3D 0) a= nd dtest.d(15):abc(T : U[N],U,ulong N)</font></div> </div></div></div><div class=3D"gmail_quote"><div class=3D"gmail_quote"><di= v><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace= "><br></font></div></div></div></blockquote><div class=3D"gmail_quote"><div= class=3D"gmail_quote"> <div><br></div></div></div> --bcaec51dd49144dc6204b0816bf6--
Oct 30 2011
prev sibling next sibling parent J Arrizza <cppgent0 gmail.com> writes:
--bcaec51dd179a0ad2604b08174c1
Content-Type: text/plain; charset=ISO-8859-1

On Sun, Oct 30, 2011 at 3:13 AM, Timon Gehr <timon.gehr gmx.ch> wrote:

 What compiler version are you using?

$ dmd -v DMD64 D Compiler v2.055 --bcaec51dd179a0ad2604b08174c1 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div>On Sun, Oct 30, 2011 at 3:13 AM, Timon Gehr <span dir=3D"ltr">&lt;<a h= ref=3D"mailto:timon.gehr gmx.ch">timon.gehr gmx.ch</a>&gt;</span> wrote:</d= iv><div class=3D"gmail_quote"><blockquote class=3D"gmail_quote" style=3D"ma= rgin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"> <div class=3D"im"><br></div> What compiler version are you using?<br> </blockquote></div><div><br></div><div>$ dmd -v</div><div>DMD64 D Compiler = v2.055</div><div><br></div> --bcaec51dd179a0ad2604b08174c1--
Oct 30 2011
prev sibling next sibling parent J Arrizza <cppgent0 gmail.com> writes:
--bcaec51dd179d027b504b085f5b3
Content-Type: text/plain; charset=ISO-8859-1

On Sun, Oct 30, 2011 at 7:36 AM, Andrei Alexandrescu <
SeeWebsiteForEmail erdani.org> wrote:

 The code as above is canonical. I think restricted templates are the way
 to go for most code.

Yes, they are much simpler to use. I went back to traits.d to see how isDynamicArray and isStaticArray were built mostly to figure out the patterns used for them. I found a couple more isArray!T and isSomeString which simplify and generalize the code just a little more: import std.stdio; import std.traits; void abc(T) (T parm1) if (isSomeString!T || !isArray!T) { writeln("simpleparm: ", parm1); } void abc(T) (T parm1) if (!isSomeString!T && isArray!T) { writeln("array : ", parm1); } void main(string[] args) { writeln("v4"); abc(1); abc("str"); int[] arr = [1, 2]; abc(arr); int[2] arr2 = [3, 4]; abc(arr2); } Another one that looked promising is isIterable() for the array. All of this begs the question, where do I find the latest doc? Since these are not showing up in the online doc but are clearly in traits.d. http://d-programming-language.org/traits.html Thanks again for your help, John --bcaec51dd179d027b504b085f5b3 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">On Sun, Oct 30, 2011 at 7:36 AM, Andrei Alexandr= escu <span dir=3D"ltr">&lt;<a href=3D"mailto:SeeWebsiteForEmail erdani.org"=
SeeWebsiteForEmail erdani.org</a>&gt;</span> wrote:<br><div class=3D"gmail=

<blockquote class=3D"gmail_quote" style=3D"margin-top: 0px; margin-right: 0= px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-= left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex= ; "> <div class=3D"im">The code as above is canonical. I think restricted templa= tes are the way to go for most code.</div></blockquote></div><div><br></div=
<div>Yes, they are much simpler to use.=A0</div><div><br></div><div>I went=

stly to figure out the patterns used for them.=A0I =A0found a couple more = =A0isArray!T =A0and isSomeString which simplify and generalize the code jus= t a little more:</div> <div><br></div></div><blockquote class=3D"webkit-indent-blockquote" style= =3D"margin: 0 0 0 40px; border: none; padding: 0px;"><div class=3D"gmail_qu= ote"><div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#3= 9;, monospace"><div> import std.stdio;</div><div>import std.traits;</div><div>void abc(T) (T par= m1)</div><div>=A0 if (isSomeString!T || !isArray!T)</div><div>=A0 {</div><d= iv>=A0 writeln(&quot;simpleparm: &quot;, parm1);</div><div>=A0 }</div><div>= void abc(T) (T parm1)</div> <div>=A0 if (!isSomeString!T &amp;&amp; isArray!T)</div><div>=A0 {</div><di= v>=A0 writeln(&quot;array : &quot;, parm1);</div><div>=A0 }</div><div>void = main(string[] args)</div><div>=A0 {</div><div>=A0 =A0 writeln(&quot;v4&quot= ;);</div><div> =A0 =A0 abc(1);</div><div>=A0 =A0 abc(&quot;str&quot;);</div><div>=A0 =A0 i= nt[] arr =3D [1, 2];</div><div>=A0 =A0 abc(arr);</div><div>=A0 =A0 int[2] a= rr2 =3D [3, 4];</div><div>=A0 =A0 abc(arr2);</div><div>=A0 }</div></font></= div></div></div></blockquote> <div class=3D"gmail_quote"><div><br></div><div>Another one that looked prom= ising is isIterable() for the array.</div><div><br></div><div>All of this b= egs the question, where do I find the latest doc?=A0Since these are not sho= wing up in the online doc but are clearly in traits.d.</div> <div><a href=3D"http://d-programming-language.org/traits.html">http://d-pro= gramming-language.org/traits.html</a></div><div><br></div><div>Thanks again= for your help,</div><div>John</div><div><br></div><div><br></div></div> --bcaec51dd179d027b504b085f5b3--
Oct 30 2011
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Sunday, October 30, 2011 08:41:47 J Arrizza wrote:
 All of this begs the question, where do I find the latest doc? Since these
 are not showing up in the online doc but are clearly in traits.d.
 http://d-programming-language.org/traits.html

That page is talking about __traits (which is built into the compiler), not std.traits. For std.traits, you want http://www.d-programming-language.org/phobos/std_traits.html Depending on what you're doing, you may need both, but generally all you need is std.traits unless you're getting fancy. - Jonathan M Davis
Oct 30 2011
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Sun, 30 Oct 2011 10:36:26 -0400, Andrei Alexandrescu  
<SeeWebsiteForEmail erdani.org> wrote:

 On 10/30/11 5:16 AM, J Arrizza wrote:
         You should use either std.traits.isXxx systematically, or
         patterns systematically, but not both at the same time.
         Personally I prefer isXxx because they foster simple logic to
         decide what overloads should apply.


     Also, when posting, you may want to include complete short programs
     so others can try them quickly.


 Andrei, I thought I had posted the entire program. Here it is again
 using only traits as you recommend:

     import std.stdio;
     import std.traits;
     void abc(T) (T parm1)
        if (isNarrowString!T || (!isStaticArray!T && !isDynamicArray!T))
        {
        writeln("simpleparm: ", parm1);
        }
     void abc(T) (T parm1)
        if (!isNarrowString!T && (isDynamicArray!T || isStaticArray!T) )
        {
        writeln("array : ", parm1);
        }
     void main(string[] args)
        {
          writeln("v4");
          abc(1);
          abc("str");
          int[] arr = [1, 2];
          abc(arr);
          int[2] arr2 = [3, 4];
          abc(arr2);
        }

Thanks, sorry for having missed that. The code as above is canonical. I think restricted templates are the way to go for most code. Pattern matching on types is rather arcane and should be let to a few advanced uses (such as implementing traits themselves).

The attraction of using specializations instead of constraints (aside from the readability) is that specializations do not require modifying the non-specialized templates. For example: void abc(T)(T parm1) {...} void abc(T:int)(T parm1) {...} vs: void abc(T)(T parm1) if (!is(T : int)) {...} void abc(T)(T parm1) if (is(T: int)) {...} Note that the if(!is(T :int)) is required in the base case. This can quickly get out of hand if you have lots of specializations (see any phobos modules for lots of examples). It might be nice if constrained templates took precedence over non-constrained ones. -Steve
Oct 31 2011