Thread overview | |||||
---|---|---|---|---|---|
|
December 09, 2004 function overloading and mixins | ||||
---|---|---|---|---|
| ||||
I think its been brought up before that since mixins do not override a function declaration in its scope with a function in the template of the same name, you cannot use mixins for copying useful functions for similar types. in C++ you can use template<class T>T max( T a, T b ) { if ( a < b ) return b; else return a; } In D the way would be: template Algo(T) { T max( T a, T b ) { if ( a < b ) return b; else return a; } } mixin Algo!(float); mixin Algo!(int); ... This does not seem to work since mixins are not aware of different function signatures, they seem to just look at names. I don't see an easy way to accomplish this in D, unless I am missing something. IMO this is a mistake not to add before 1.0 to make D competitive with C++. |
December 09, 2004 Re: function overloading and mixins | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Medlock | > template Algo(T)
> {
> T max( T a, T b ) { if ( a < b ) return b; else return a; }
> }
>
> mixin Algo!(float);
> mixin Algo!(int);
To overload use alias:
mixin Algo!(float);
mixin Algo!(int);
alias Algo!(float).max max;
alias Algo!(int).max max;
Or if you substitute "max" for "Algo" it looks a little nicer:
template max(T)
{
T max( T a, T b ) { if ( a < b ) return b; else return a; }
}
class Foo {
// don't even need to mix in
alias .max!(float) max;
alias .max!(int) max;
}
int main() {
Foo x = new Foo;
x.max(10,10);
x.max(1.0,3.4);
return 0;
}
Then again I'm not sure why you are mixing in a max template since just
leaving it in the global scope seems simpler:
template max(T)
{
T max( T a, T b ) { if ( a < b ) return b; else return a; }
}
int main() {
max!(int)(10,10);
max!(float)(1.0,3.4);
return 0;
}
but then you are probably just making up this example to simplify the
posting, no?
-Ben
|
December 09, 2004 Re: function overloading and mixins | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | Ben Hinkle wrote: >>template Algo(T) >>{ >> T max( T a, T b ) { if ( a < b ) return b; else return a; } >>} >> >>mixin Algo!(float); >>mixin Algo!(int); > > > To overload use alias: > mixin Algo!(float); > mixin Algo!(int); > alias Algo!(float).max max; > alias Algo!(int).max max; > Thanks for the heads up on the alias, I will try it. > Then again I'm not sure why you are mixing in a max template since just > leaving it in the global scope seems simpler: > template max(T) > { > T max( T a, T b ) { if ( a < b ) return b; else return a; } > } > int main() { > max!(int)(10,10); > max!(float)(1.0,3.4); > return 0; > } > but then you are probably just making up this example to simplify the > posting, no? > > -Ben > Yes the example is a bit simpler. I am doing a 3d Math package centered around OpenGL which needs 2,3,4 size Vectors of different types(mostly just int and float). I was trying to get top level functions like dot( a, b ) which seem more natural to me than a.dot(b), not to mention they match the OpenGL shading language built in functions. It still seems a little strange the mixin looks at the name rather than the type signature, though. Tbanks again for the help. -David |
Copyright © 1999-2021 by the D Language Foundation