Can't understand templates
Ali Çehreli via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Nov 29 12:16:23 PST 2014
On 11/29/2014 10:19 AM, Sly wrote:
> You miss another definition which introduces a conflict:
> T getResponse(T)(string question)
> {...}
The following works with dmd git head:
import std.stdio;
T getResponse(T)(string question)
{
writef("%s (%s): ", question, T.stringof);
T response;
readf(" %s", &response);
return response;
}
class Pair(A, B) {
A a;
B b;
this(A a, B b) {this.a = a; this.b = b;}
}
Pair!(A, B) getResponse(P : Pair!(A, B), A, B)(string question)
{
writeln(question);
auto a = getResponse!A(" a");
auto b = getResponse!B(" b");
return new Pair!(A, B)(a, b);
}
struct Point(T)
{
T x;
T y;
}
Point!T getResponse(P : Point!T, T)(string question)
{
writefln("%s (Point!%s)", question, T.stringof);
auto x = getResponse!T(" x");
auto y = getResponse!T(" y");
return Point!T(x, y);
}
void main()
{
auto number = getResponse!int("number?");
writeln(number);
auto pair = getResponse!(Pair!(int, long))("pair?");
writeln(pair);
auto point = getResponse!(Point!int)("point?");
writeln(point);
}
Ali
More information about the Digitalmars-d-learn
mailing list