www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Implicit fn template instantiation

reply anthony.difranco yale.edu writes:
Can someone explain why I can't instantiate this template implicitly andor
comment on whether I'm overlooking a better way to accomplish what this template
does?

template increment(T, K)
{
void increment(T[K] association, K key)
{
if(key in association)
{
association[key]++;
}
else
{
association[key] = 1;
}
}
}

Thanks,
Anthony
Mar 23 2006
next sibling parent Sean Kelly <sean f4.ca> writes:
anthony.difranco yale.edu wrote:
 Can someone explain why I can't instantiate this template implicitly andor
 comment on whether I'm overlooking a better way to accomplish what this
template
 does?

Mostly because ITI is still incomplete. Anything beyond strict type replacement doesn't really work. Sean
Mar 23 2006
prev sibling parent reply BCS <BCS_member pathlink.com> writes:
In article <dvvk0i$2tum$1 digitaldaemon.com>, anthony.difranco yale.edu says...
Can someone explain why I can't instantiate this template implicitly andor
comment on whether I'm overlooking a better way to accomplish what this template
does?

template increment(T, K)
{
void increment(T[K] association, K key)
{
if(key in association)
{
association[key]++;
}
else
{
association[key] = 1;
}
}
}

Thanks,
Anthony

IIRC if(T* val = (key in association)) (*val)++; else association[key]=1; haven't tried it but it should work
Mar 23 2006
parent reply Oskar Linde <oskar.lindeREM OVEgmail.com> writes:
BCS wrote:
 In article <dvvk0i$2tum$1 digitaldaemon.com>, anthony.difranco yale.edu says...
 Can someone explain why I can't instantiate this template implicitly andor
 comment on whether I'm overlooking a better way to accomplish what this
template
 does?

 template increment(T, K)
 {
 void increment(T[K] association, K key)
 {
 if(key in association)
 {
 association[key]++;
 }
 else
 {
 association[key] = 1;
 }
 }
 }

 Thanks,
 Anthony

IIRC if(T* val = (key in association)) (*val)++; else association[key]=1; haven't tried it but it should work

Yes. A full working version with the current limited IFTI would look like: template increment(T,K) { static assert(is(K : typeof(T.init.keys[0]))); void increment(inout T association, K key) { if(auto val = (key in association)) (*val)++; else association[key]=1; } } /Oskar
Mar 24 2006
parent anthony.difranco yale.edu writes:
Thanks; it works.

In article <e00fq4$131r$1 digitaldaemon.com>, Oskar Linde says...
BCS wrote:
 In article <dvvk0i$2tum$1 digitaldaemon.com>, anthony.difranco yale.edu says...
 Can someone explain why I can't instantiate this template implicitly andor
 comment on whether I'm overlooking a better way to accomplish what this
template
 does?

 template increment(T, K)
 {
 void increment(T[K] association, K key)
 {
 if(key in association)
 {
 association[key]++;
 }
 else
 {
 association[key] = 1;
 }
 }
 }

 Thanks,
 Anthony

IIRC if(T* val = (key in association)) (*val)++; else association[key]=1; haven't tried it but it should work

Yes. A full working version with the current limited IFTI would look like: template increment(T,K) { static assert(is(K : typeof(T.init.keys[0]))); void increment(inout T association, K key) { if(auto val = (key in association)) (*val)++; else association[key]=1; } } /Oskar

Mar 27 2006