June 12, 2009 Re: array depth template | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | On Thu, Jun 11, 2009 at 11:45 PM, Saaa<empty@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 '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 Um, there's no "one's better than the other." Don't overgeneralize on things you don't understand. Implicit conversion is necessary in some cases, and in others, exact comparison is needed. >>> 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. A function template is just a function in a template. Templates can work just fine on their own. If you're manipulating types, there's really no need to get functions involved. >> 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 ? See "Implicit Template Properties" here: http://www.digitalmars.com/d/1.0/template.html When you create a member in a template with the same name as the template, you can access it from the template without explicitly writing ArrayDepth!(int[]).ArrayDepth. |
June 12, 2009 Re: array depth template | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | >
> 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 = `, BaseType!(T).stringof );
btw. Thanks for everything !!
It's nice to get some comments after a fews days of struggling with them
templates :)
I can now parse any numeric type (array) till depth 4; still haven't found a way to generalize this part :(
private void Parse(T)(ref T parsed)
{
..
switch( depth )
{
case 0:
if( temp.length < index[depth] ) temp.length = temp.length * 2;
break;
static if( is(T A:A[][]))
{
case 1:
if( temp[ index[0] ].length < index[depth] ) temp[index[0]].length =
temp[index[0]].length * 2;
break;
}
static if( is(T A:A[][][]))
{
case 2:
if( temp[ index[0] ][ index[1] ].length < index[depth] ) temp[ index[0] ][
index[1] ].length = temp[ index[0] ][ index[1] ].length * 2;
break;
}
default:
assert(false);
break;
}
..
}
|
June 12, 2009 Re: array depth template | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | >Um, there's no "one's better than the other." Don't overgeneralize on things you don't understand. Implicit conversion is necessary in some cases, and in others, exact comparison is needed. I didn't mean it like that, don't worry :) I mean, I apparently used implicit when I actually wanted explicit. >A function template is just a function in a template. Templates can work just fine on their own. If you're manipulating types, there's really no need to get functions involved. Check >See "Implicit Template Properties" here: http://www.digitalmars.com/d/1.0/template.html Reading . . >When you create a member in a template with the same name as the template, you can access it from the template without explicitly writing ArrayDepth!(int[]).ArrayDepth. |
Copyright © 1999-2021 by the D Language Foundation