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

Re: Can't understand templates

2014-11-29 Thread Ali Çehreli via Digitalmars-d-learn
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;

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 Meta via Digitalmars-d-learn
On Saturday, 29 November 2014 at 18:19:40 UTC, Sly wrote: You miss another definition which introduces a conflict: T getResponse(T)(string question) {...} In that case, you're better off with a pair of declarations: struct Point(T) { T x; T y; } T getResponse(T)(string message

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 Meta via Digitalmars-d-learn
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 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

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 Ali Çehreli via Digitalmars-d-learn
On 11/29/2014 12:41 AM, Sly wrote: 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:

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 Ali Çehreli via Digitalmars-d-learn
On 11/28/2014 10:26 PM, Sly wrote: > I still don't understand how to > read this signature: > Point!T getResponse(T: Point!T)(string question) The following is what makes sense to me. Sorry for going step by step but I think it helped me understand your question. (See item 3 below.) 1) Point

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-28 Thread Ali Çehreli via Digitalmars-d-learn
On 11/28/2014 12:36 PM, Sly wrote: > Let's take a simpler example from earlier in the tutorial, where > Point is a non-template struct: > > > // The general definition of the function template (same as > before) > T getResponse(T)(string question) > { [...] > } That definition would work with an

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 H. S. Teoh via Digitalmars-d-learn
On Fri, Nov 28, 2014 at 07:32:38PM +, Sly via Digitalmars-d-learn wrote: > Here is an example from the tutorial: [...] > Point!T getResponse(T: Point!T)(string question) { > writefln("%s (Point!%s)", question, T.stringof); > auto x = getResponse!T(" x"); > auto y = getResponse

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) {