Can't understand templates

2014-11-28 Thread Sly via Digitalmars-d-learn
Here is an example from the tutorial: struct Point(T) { T x; T y; } T getResponse(T)(string question) { writef("%s (%s): ", question, T.stringof); T response; readf(" %s", &response); return response; } Point!T getResponse(T: Point!T)(string question) {

Re: Can't understand templates

2014-11-28 Thread Sly via Digitalmars-d-learn
On Friday, 28 November 2014 at 19:45:48 UTC, H. S. Teoh via Digitalmars-d-learn wrote: This syntax is a little confusing, but basically the ":" there is saying "this type, when instantiated with the following pattern, produces a valid type". Essentially it's equivalent to: Point!T get

Re: Can't understand templates

2014-11-28 Thread Sly via Digitalmars-d-learn
OK, so we *can* have overlapping templates in one fits better than another, just like in C++. I still don't understand how to read this signature: Point!T getResponse(T: Point!T)(string question) On Friday, 28 November 2014 at 23:59:07 UTC, Ali Çehreli wrote: On 11/28/2014 12:36 PM, Sly wrote:

Re: Can't understand templates

2014-11-29 Thread Sly via Digitalmars-d-learn
Ah, now I see. In C++ we would also need to supply template parameters, but we'd put them before return type (template getResponse>); in D we put them before the colon. Now I'm trying to write a specialization for Pair. It seems that this is the way to do it: Pair!(A, B) getResponse(A, B: Pair!(

Re: Can't understand templates

2014-11-29 Thread Sly via Digitalmars-d-learn
On Saturday, 29 November 2014 at 09:11:51 UTC, Ali Çehreli wrote: Point!T getResponse(P : Point!T, T)(string question) { // ... } This doesn't work because now this definition has 2 parameters P and T. I have to specify both like this: auto pt = getResponse!(Point!int, int)("point"); whi

Re: Can't understand templates

2014-11-29 Thread Sly via Digitalmars-d-learn
You miss another definition which introduces a conflict: T getResponse(T)(string question) {...} On Saturday, 29 November 2014 at 17:20:42 UTC, Meta wrote: On Saturday, 29 November 2014 at 11:07:34 UTC, Sly wrote: On Saturday, 29 November 2014 at 09:11:51 UTC, Ali Çehreli wrote: Point!T getR

Re: Can't understand templates

2014-11-29 Thread Sly via Digitalmars-d-learn
This is clearly an incorrect way because it requires editing the original definition every time a new class is introduced. On Saturday, 29 November 2014 at 19:10:34 UTC, Meta wrote: On Saturday, 29 November 2014 at 18:19:40 UTC, Sly wrote: You miss another definition which introduces a conflict

Re: Can't understand templates

2014-11-29 Thread Sly via Digitalmars-d-learn
OK. I'm using gdc 4.8.2 and this doesn't compile, so it's probably an old version. Checked on ideone and it works with dmd-2.042. Thanks a lot for your help! On Saturday, 29 November 2014 at 20:16:24 UTC, Ali Çehreli wrote: On 11/29/2014 10:19 AM, Sly wrote: > You miss another definition which