[Issue 4167] New: Template overrides with alias
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sun May 9 07:02:09 PDT 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4167
Summary: Template overrides with alias
Product: D
Version: future
Platform: All
OS/Version: All
Status: NEW
Severity: enhancement
Priority: P2
Component: DMD
AssignedTo: nobody at puremagic.com
ReportedBy: bearophile_hugs at eml.cc
--- Comment #0 from bearophile_hugs at eml.cc 2010-05-09 07:02:05 PDT ---
This is an enhancement request, but I am not sure if this can be done, and even
if it's doable I don't know if this is a good idea. If it's hard to implement
or it's not a good idea you can close this.
This is correct D2 code that uses an alias to emulate a new override of
function foo:
int foo(int x) {
return 10;
}
int bar(int x, int y) {
return 20;
}
alias bar foo;
void main() {
int x1 = foo(1); // OK
int x2 = bar(2, 100); // OK
int x3 = foo(3, 100); // OK
}
But similar code with templates doesn't work, and produces (dmd 2.043):
test.d(7): Error: alias test.Foo conflicts with template test.Foo(int x) at
test.d(1)
template Foo(int x) {
enum int Foo = 10;
}
template Bar(int x, int y) {
enum int Bar = 10;
}
alias Bar Foo; // line 7, Error
void main() {
int x1 = Foo!(1); // OK
int x2 = Bar!(2, 100); // OK
int x3 = Foo!(3, 100); // Not OK
}
To do that I have to create a wrapper, this seems to work:
template Foo(int x) {
enum int Foo = 10;
}
template Bar(int x, int y) {
enum int Bar = 10;
}
template Foo(int x, int y) {
enum int Foo = Bar!(x, y);
}
void main() {
int x1 = Foo!(1); // OK
int x2 = Bar!(2, 100); // OK
int x3 = Foo!(3, 100); // OK
}
Is it possible to change the compiler so an alias can be used to emulate
template overloads?
I have asked on IRC and two persons have said that such usage of alias with
templates can be a little confusing.
This usage of the alias can be seen useful for a simple form of Partial
Template Application, similar to template currying:
import std.stdio: writeln;
import std.conv: to;
import std.algorithm: array, map;
template to_(Tout) {
Tout to_(Tin)(Tin x) {
return to!(Tout, Tin)(x);
}
}
alias to to_; // Currently an error here
void main() {
string[] data = ["1", "2", "3"];
int[] arr = array(map!(to_!int)(data));
writeln(to_!int("10"));
}
Currently the map! needs a full template specialization:
map!(to!(int, string))(data)
While the writeln can use half specialization:
writeln(to!int("10"));
Defining that to_ plus using an alias I can create a partial template.
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list