digitalmars.D.learn - Alias vs templates
- Jean-Louis Leroy (24/24) Feb 21 2018 I am trying to figure out a crispier syntax for templatized open
- Steven Schveighoffer (9/41) Feb 21 2018 I think because one is a function, which is allowed to be overloaded,
- Jean-Louis Leroy (8/18) Feb 21 2018 I just discovered that this works:
I am trying to figure out a crispier syntax for templatized open methods. I am stumbling on this (see comments): // dmd -run conflict.d int foo(); struct Foo { static int foo(int x) { return x; } } alias foo = Foo.foo; // overload with an alias - OK int bar(T)(); int bar(T)(T x) { return x; } // overloaded function templates - OK int baz(T)(); struct Baz { static int baz(T)(T x) { return x; } } //alias baz = Baz.baz; // Error: alias conflict.baz conflicts with template conflict.baz(T)() at conflict.d(11) void main() { import std.stdio; writeln(foo(42)); // 42 writeln(bar(666)); // 666 } Why?
Feb 21 2018
On 2/21/18 1:46 PM, Jean-Louis Leroy wrote:I am trying to figure out a crispier syntax for templatized open methods. I am stumbling on this (see comments): // dmd -run conflict.d int foo(); struct Foo { static int foo(int x) { return x; } } alias foo = Foo.foo; // overload with an alias - OK int bar(T)(); int bar(T)(T x) { return x; } // overloaded function templates - OK int baz(T)(); struct Baz { static int baz(T)(T x) { return x; } } //alias baz = Baz.baz; // Error: alias conflict.baz conflicts with template conflict.baz(T)() at conflict.d(11) void main() { import std.stdio; writeln(foo(42)); // 42 writeln(bar(666)); // 666 } Why?I think because one is a function, which is allowed to be overloaded, the other is a symbol. But clearly, there are some liberties taken when you are overloading function templates, as overloading them works just fine. I think this may have been a patch at some point (you didn't use to be able to overload template functions with normal functions). It seems like a reasonable request that this code should work. -Steve
Feb 21 2018
On Wednesday, 21 February 2018 at 20:27:29 UTC, Steven Schveighoffer wrote:On 2/21/18 1:46 PM, Jean-Louis Leroy wrote:I just discovered that this works: int baz(T)(); struct Baz { static int baz(T)(T x) { return x; } } alias baz(T) = Baz.baz!T;[...]I think because one is a function, which is allowed to be overloaded, the other is a symbol. But clearly, there are some liberties taken when you are overloading function templates, as overloading them works just fine. I think this may have been a patch at some point (you didn't use to be able to overload template functions with normal functions). It seems like a reasonable request that this code should work.
Feb 21 2018