How is this code supposed to work?

AndreasDavour andreas.davour at protonmail.ch
Fri Jan 25 09:34:47 UTC 2019


I'm reading the "Programming D" here:
http://ddili.org/ders/d.en/templates.html
and am a bit confused by the section on templates.

with a struct template like this:
struct Point(T) {
   T x;
   T y;

   T distanceTo(Point that) const {
     immutable real xDistance = x - that.x;
     immutable real yDistance = y - that.y;

     immutable distance = sqrt((xDistance * xDistance) +
			      (yDistance * yDistance));
     return cast(T) distance;
   }
}

and a function template like this:
Point!T getResponse(T : Point!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);
}

How am I supposed to use that??

   auto point3 = getResponse!Point!int("What's the point? ");
   auto point4 = getResponse!Point!int("What's the point? ");
   writeln("Distance: ", point3.distanceTo(point4));

generates the error:

struct_templates.d(48): Error: multiple ! arguments are not 
allowed
struct_templates.d(49): Error: multiple ! arguments are not 
allowed

Which makes me wonder about the syntax.



More information about the Digitalmars-d-learn mailing list