www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Problem with template functions

reply Olli Aalto <oaalto gmail.com> writes:
Hi!

I've gotten myself into a jam. I've written several templated functions 
which work correctly inside their own modules. I have two modules with 
different kinds of functions, but which couple have the same name ond/or 
alias.

I've written a small example of the problem:

module1.d:
module module1;

private void _templateFuntion(int T, A...)(int[T][T] arr, A args)
{
}
public alias _templateFuntion!(2, int, int) func;


module2.d:
module module2;

import module1;

private void _templateFuntion(int T, A...)(int[T] arr, A args)
{
}
public alias _templateFuntion!(2, int, int, int) func;

void main()
{
     int[2][2] array;
     array.func(2, 3);
}

Compiler output:
module2.d(14): function 
module2._templateFuntion!(2,int,int,int)._templateFuntion (int[2],(int, 
int, int)) does not match parameter types (int[2][2],int,int)
module2.d(14): Error: cannot implicitly convert expression (array) of 
type int[2][2] to int[2]
module2.d(14): Error: expected 4 arguments, not 3

For some reason the compiler doesn't see the template function from module1.

Am I doing something wrong or is this something that is just not yet 
supported?

O.
Jan 11 2007
next sibling parent Lutger <lutger.blijdestijn gmail.com> writes:
Olli Aalto wrote:
 Hi!
 
 I've gotten myself into a jam. I've written several templated functions 
 which work correctly inside their own modules. I have two modules with 
 different kinds of functions, but which couple have the same name ond/or 
 alias.
 
 I've written a small example of the problem:
 
 module1.d:
 module module1;
 
 private void _templateFuntion(int T, A...)(int[T][T] arr, A args)
 {
 }
 public alias _templateFuntion!(2, int, int) func;
 
 
 module2.d:
 module module2;
 
 import module1;
 
 private void _templateFuntion(int T, A...)(int[T] arr, A args)
 {
 }
 public alias _templateFuntion!(2, int, int, int) func;
 
 void main()
 {
     int[2][2] array;
     array.func(2, 3);
 }
 
 Compiler output:
 module2.d(14): function 
 module2._templateFuntion!(2,int,int,int)._templateFuntion (int[2],(int, 
 int, int)) does not match parameter types (int[2][2],int,int)
 module2.d(14): Error: cannot implicitly convert expression (array) of 
 type int[2][2] to int[2]
 module2.d(14): Error: expected 4 arguments, not 3
 
 For some reason the compiler doesn't see the template function from 
 module1.
 
 Am I doing something wrong or is this something that is just not yet 
 supported?
 
 O.

I believe this (overloading function templates) is not supported, at least it wasn't some versions ago and I doubt it is changed.
Jan 11 2007
prev sibling parent reply BCS <BCS pathlink.com> writes:
Olli Aalto wrote:
 Hi!
 
 I've gotten myself into a jam. I've written several templated functions 
 which work correctly inside their own modules. I have two modules with 
 different kinds of functions, but which couple have the same name ond/or 
 alias.
 
 I've written a small example of the problem:
 
 module1.d:
 module module1;
 
 private void _templateFuntion(int T, A...)(int[T][T] arr, A args)
 {
 }
 public alias _templateFuntion!(2, int, int) func;
 
 
 module2.d:
 module module2;
 
 import module1;
 
 private void _templateFuntion(int T, A...)(int[T] arr, A args)
 {
 }
 public alias _templateFuntion!(2, int, int, int) func;
 
 void main()
 {
     int[2][2] array;
     array.func(2, 3);
 }
 
 Compiler output:
 module2.d(14): function 
 module2._templateFuntion!(2,int,int,int)._templateFuntion (int[2],(int, 
 int, int)) does not match parameter types (int[2][2],int,int)
 module2.d(14): Error: cannot implicitly convert expression (array) of 
 type int[2][2] to int[2]
 module2.d(14): Error: expected 4 arguments, not 3
 
 For some reason the compiler doesn't see the template function from 
 module1.
 
 Am I doing something wrong or is this something that is just not yet 
 supported?
 
 O.

You might try using the fully qualified name "module1._templateFuntion". If you need to have them truly overload, e.i. using a tuple as the parameters, I haven't seen that work. I think that it should be doable, but it isn't. It would open up a a lot of functionality. Something with mixins would be my choice (like can already be done with functions).
Jan 11 2007
parent Olli Aalto <odeamus dystopia.fi> writes:
BCS wrote:
 Olli Aalto wrote:
 Hi!

 I've gotten myself into a jam. I've written several templated 
 functions which work correctly inside their own modules. I have two 
 modules with different kinds of functions, but which couple have the 
 same name ond/or alias.

 I've written a small example of the problem:

 module1.d:
 module module1;

 private void _templateFuntion(int T, A...)(int[T][T] arr, A args)
 {
 }
 public alias _templateFuntion!(2, int, int) func;


 module2.d:
 module module2;

 import module1;

 private void _templateFuntion(int T, A...)(int[T] arr, A args)
 {
 }
 public alias _templateFuntion!(2, int, int, int) func;

 void main()
 {
     int[2][2] array;
     array.func(2, 3);
 }

 Compiler output:
 module2.d(14): function 
 module2._templateFuntion!(2,int,int,int)._templateFuntion 
 (int[2],(int, int, int)) does not match parameter types 
 (int[2][2],int,int)
 module2.d(14): Error: cannot implicitly convert expression (array) of 
 type int[2][2] to int[2]
 module2.d(14): Error: expected 4 arguments, not 3

 For some reason the compiler doesn't see the template function from 
 module1.

 Am I doing something wrong or is this something that is just not yet 
 supported?

 O.

You might try using the fully qualified name "module1._templateFuntion". If you need to have them truly overload, e.i. using a tuple as the parameters, I haven't seen that work. I think that it should be doable, but it isn't. It would open up a a lot of functionality. Something with mixins would be my choice (like can already be done with functions).

Actually it's funny that it doesn't work in this case. If I name the functions with unique names and then use aliases the create different versions of them, but have the aliases have the same name. I'd think that the aliases would be different. Or is this a problem with the protection attributes? For example: module1: private void function1(int I, A...)(int[I] v, A args) {} public alias function1!(2, int) function; public alias function1!(2, int, int) function; Now calling: int[2] array; array.function(1, 2); In the module1 that works, but aren't those two aliases really like this: void function1(int[2], int i) and void function1(int[2], int i, int j) So when we add the second module like this: module2: private void function1(int I, A...)(int[I][I) m, A args) {} public alias function1!(2, int) function; public alias function1!(2, int, int) function; Aren't those then these: void function1(int[2][2], int i) and void function1(int[2][2], int i, int j) ?? Shouldn't that work? They point to different functions in a way. Just wondering as my 2 days of template and array practice didn't work in the end. :/ At least I learned something. :) O.
Jan 11 2007