digitalmars.D.bugs - function overloading and mixins
- David Medlock (22/22) Dec 09 2004 I think its been brought up before that since mixins do not override a
- Ben Hinkle (35/41) Dec 09 2004 To overload use alias:
- David Medlock (12/43) Dec 09 2004 Yes the example is a bit simpler. I am doing a 3d Math package centered...
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++.
Dec 09 2004
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
Dec 09 2004
Ben Hinkle wrote:Thanks for the heads up on the alias, I will try it.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;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? -BenYes 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
Dec 09 2004