array depth template

2009-06-11 Thread Saaa
Is this a good way to get the depth of an array? int getArrayDepth(T)(ref T array) { static if( is(T A:A[]) ) { A arr; return 1 + getArrayDepth(arr); } else { return 0; } return -1; }

Re: array depth template

2009-06-11 Thread BCS
Hello Saaa, Is this a good way to get the depth of an array? int getArrayDepth(T)(ref T array) { static if( is(T A:A[]) ) { A arr; return 1 + getArrayDepth(arr); } else { return 0; } return -1; } I just posted this today http://www.dsource.org/projects/scrapple/browser/trunk/Serial/utill.d

Re: array depth template

2009-06-11 Thread Jarrett Billingsley
On Thu, Jun 11, 2009 at 9:15 PM, Saaaem...@needmail.com wrote: Is this a good way to get the depth of an array? int getArrayDepth(T)(ref T array) { static if( is(T A:A[]) ) { A arr; return 1 + getArrayDepth(arr); } else { return 0; } return -1; } It's kind of the right idea, but..

Re: array depth template

2009-06-11 Thread Jarrett Billingsley
On Thu, Jun 11, 2009 at 11:02 PM, Saaaem...@needmail.com wrote: Any advantage to using two? I just tend to prefer template specialization when doing type pattern matching. It works out better than is() in some cases. He also does : is( T B ==B[]) iso is( T B:B[] ) Any significant

Re: array depth template

2009-06-11 Thread Saaa
Any advantage to using two? I just tend to prefer template specialization when doing type pattern matching. It works out better than is() in some cases. Looks very Haskell like :) He also does : is( T B ==B[]) iso is( T B:B[] ) Any significant difference there? I'm.. not sure, in this

Re: array depth template

2009-06-11 Thread Saaa
It's kind of the right idea, but.. it's also kind of weird. template ArrayDepth(T: T[]) { const ArrayDepth = 1 + ArrayDepth!(T); } template ArrayDepth(T) { const ArrayDepth = 0; } This lets you get the depth of any array _type_, like ArrayDepth!(int[][]) gives 2. Although I don't

Re: array depth template

2009-06-11 Thread Saaa
template BaseType(T: T[]) { const BaseType = BaseType!(T); } template BaseType(T) {const BaseType = T; } .. else static if( std2.traits.isNumeric!( BaseType!(T) ) ) { .. ddata\ddata.d(192): template instance isNumeric!(int) does not match any template declaration ddata\ddata.d(192): Error:

Re: array depth template

2009-06-11 Thread Jarrett Billingsley
On Fri, Jun 12, 2009 at 12:04 AM, Saaaem...@needmail.com wrote: template BaseType(T: T[]) { const BaseType = BaseType!(T); } template BaseType(T) {const BaseType = T; } .. else static if( std2.traits.isNumeric!( BaseType!(T) ) ) { .. ddata\ddata.d(192): template instance isNumeric!(int)

Re: array depth template

2009-06-11 Thread Jarrett Billingsley
On Thu, Jun 11, 2009 at 11:45 PM, Saaaem...@needmail.com wrote: I'm.. not sure, in this case anyway.  Normally == does strict type comparison while : does implicit type conversion, but in this case, is() is being (ab)used to pick apart a type rather than test one.  I think it'll always return

Re: array depth template

2009-06-11 Thread Saaa
Your template. Types are not values, you cannot declare a constant that is a type. You have to use alias instead: template BaseType(T: T[]) { alias BaseType!(T) BaseType; } template BaseType(T) { alias T BaseType; } Erm, why did this print corretly? writefln(`BaseType = `,