Jump to page: 1 2
Thread overview
array depth template
Jun 12, 2009
Saaa
Jun 12, 2009
BCS
Jun 12, 2009
Saaa
Jun 12, 2009
Saaa
Jun 12, 2009
Saaa
Jun 12, 2009
Saaa
Jun 12, 2009
Saaa
Jun 12, 2009
Saaa
Jun 12, 2009
Saaa
June 12, 2009
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;
}


June 12, 2009
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


June 12, 2009
On Thu, Jun 11, 2009 at 9:15 PM, Saaa<empty@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.. 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.
June 12, 2009
>
> 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.

Ah , that's how you make it do at compile time, using const.

BCS also did it a bit like me (one template that is) I just didn't know you
could also just pass the type :D
Any advantage to using two?

He also does :  is( T B ==B[])  iso  is( T B:B[] )
Any significant difference there?


June 12, 2009
Also, what's the advantage of explicitly defining it as a template?


June 12, 2009
On Thu, Jun 11, 2009 at 11:02 PM, Saaa<empty@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 difference there?

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 'true' in either case if T is an array, so I don't think there's a functional difference.

> Also, what's the advantage of explicitly defining it as a template?

As opposed to what, implicitly defining it as a template?  This question doesn't really make sense.
June 12, 2009
>> 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 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 'true' in either case if T is an array, so I don't think there's a functional difference.

Implicit convertion sounds a bit dangerous, might start using == instead

>
>> Also, what's the advantage of explicitly defining it as a template?
>
> As opposed to what, implicitly defining it as a template?  This question doesn't really make sense.

I mean, I was using a function template.

> template ArrayDepth(T: T[]) { const ArrayDepth = 1 + ArrayDepth!(T); }
> template ArrayDepth(T)       { const ArrayDepth = 0; }

The code looks a bit strange to me:
ArrayDepth is both a (const) value and template name and where is the return
value ?


June 12, 2009
> 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 fully understand your code, I think I modified it correctly to get the base type of an array :)

template BaseType(T: T[]) { const BaseType = BaseType!(T); }
template BaseType(T) {const BaseType = T; }


June 12, 2009
>
> 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: expression isNumeric!(int) is not constant or
does not evaluate to a bool

What am I doing wrong?


June 12, 2009
On Fri, Jun 12, 2009 at 12:04 AM, Saaa<empty@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) does not match any
> template declaration
> ddata\ddata.d(192): Error: expression isNumeric!(int) is not constant or
> does not evaluate to a bool
>
> What am I doing wrong?

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; }
« First   ‹ Prev
1 2