www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Template argument type auto detect

reply nail <nail_member pathlink.com> writes:
Hello all,

Who can say why impossible to do following

class Class
{
template foo(T)
{
bit foo(T a, T b)
{
return a == b;
}
}
}

int main ( char [] [] args )
{
int x, y;
Class c;
c.foo(x, y); // call

return 0;
}

C:/dmd/bin/dmd -I. -IC:\dmd\src\phobos -Isrc -c -ofexamples\src\test\test.obj
examples\src\test\test.d
examples\src\test\test.d(124): c.foo is not a declaration
examples\src\test\test.d(124): function expected before (), not 'int'

Why compiler can't replase call line with c.foo!(int)(x, y) automaticly like in
C++?
Jan 21 2005
next sibling parent "Lionello Lunesu" <lionello.lunesu crystalinter.remove.com> writes:
I think you need to 'instanciate' the template with a "!", so foo!() or so. 
Dunno, never done it before.

L. 
Jan 21 2005
prev sibling parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
nail wrote:

Hello all,

Who can say why impossible to do following

class Class
{
  
//Templates are like a class/namespace in D
template foo(T)
{
bit foo(T a, T b)
{
return a == b;
}
}
}

int main ( char [] [] args )
{
int x, y;
  
Class c;
  
//Should be (something like) Class.foo!(int) c = new Class.foo!(int)();
c.foo(x, y); // call
  
There is no automatic type induction in D.
return 0;
}

C:/dmd/bin/dmd -I. -IC:\dmd\src\phobos -Isrc -c -ofexamples\src\test\test.obj
examples\src\test\test.d
examples\src\test\test.d(124): c.foo is not a declaration
examples\src\test\test.d(124): function expected before (), not 'int'

Why compiler can't replase call line with c.foo!(int)(x, y) automaticly like in
C++?


  
Jan 21 2005
parent reply nail <nail_member pathlink.com> writes:
There is no automatic type induction in D.
Why? What is the hindrance?
Jan 21 2005
parent J Anderson <REMOVEanderson badmama.com.au> writes:
nail wrote:

There is no automatic type induction in D.

    
Why? What is the hindrance?
1) Walter's against it. 2) It makes the compiler harder to write. 3) There are apparently some un-expected side-effects. As for me, I would love to have some form of automatic induction for templates. -- -Anderson: http://badmama.com.au/~anderson/ -- -Anderson: http://badmama.com.au/~anderson/
Jan 21 2005