www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Templated function as associative array property

reply biozic <dransic free.fr> writes:
This problem might have been raised before, but I can't why this doesn't 
compile (except the last line), while a non-template version of the same 
code works well. Is this a bug?

---
module test;

template Graph(T)
{
     alias T[][T] Graph;
}

void add_edge(T)(ref Graph!(T) graph, T source, T target)
{
     graph[source] ~= target;
}

void main()
{
     Graph!(string) graph;

     graph.add_edge("A", "B");           // Error
     graph.add_edge!(string)("A", "B");  // Error
     add_edge(graph, "A", "B");          // Error
     add_edge!(string)(graph, "A", "B"); // OK
}
---

DMD 2.040, Mac OS X

Thanks,
Nicolas
Mar 07 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
biozic:

 This problem might have been raised before, but I can't why this doesn't 
 compile (except the last line), while a non-template version of the same 
 code works well. Is this a bug?
I don't know why your code doesn't work, someone more expert than me can answer you. Maybe it's a bug. In the meantime this works: template Graph(T) { alias T[][T] Graph; } void add_edge(G, T)(ref G graph, T source, T target) if (is(G == Graph!T)) { graph[source] ~= target; // this is a slow operation } void main() { Graph!(string) graph; graph.add_edge("A", "B"); } Bye, bearophile
Mar 07 2010
parent reply biozic <dransic free.fr> writes:
Le 08/03/10 01:27, bearophile a écrit :
 I don't know why your code doesn't work, someone more expert than me can
answer you. Maybe it's a bug.
 In the meantime this works:
Yes, it works, thanks! But I believe this is just a workaround hack.
 graph[source] ~= target; // this is a slow operation
Yes, it's not the smartest graph implementation for D anyway :) Nicolas
Mar 08 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
biozic:
 Yes, it works, thanks! But I believe this is just a workaround hack.
It's a workaround but it's not a hack. If no one else answers you then you can add a bug to bugzilla... (I am not sure it's a real bug). Bye, bearophile
Mar 08 2010
parent reply Nicolas <biozic free.fr> writes:
bearophile Wrote:
 biozic:
 Yes, it works, thanks! But I believe this is just a workaround hack.
It's a workaround but it's not a hack. If no one else answers you then you can add a bug to bugzilla... (I am not sure it's a real bug).
Yes, you're right. I think the problem can eventually be reduced to the following simple things: --- module test; void foo(T)(T[] array) {} template Type(T) { alias T Type; } void bar(T)(Type!T t) {} void main() { int[] a = [1, 2, 3]; foo(a); // OK foo!int(a); // OK a.foo(); // OK //a.foo!int(); // Error: foo(a) isn't a template int b = 1; bar!int(b); // OK bar(b); // Error (template deduction fails) } --- The first error looks like a bug. The second one: I don't know if the template deduction engine is supposed to resolve this.
Mar 08 2010
next sibling parent bearophile <bearophileHUGS lycos.com> writes:
Nicolas:
 The first error looks like a bug.
Or maybe it's a limit of the compiler, it can even be a desired limit of the syntax. I don't like to write that.
 The second one: I don't know if the template deduction engine is supposed to
resolve this.<
I think the answer is positive. So I suggest you to file two bugs (even if the first one is not a bug, it can be useful to add a note to the D docs that explain this syntax is not allowed). Bye, bearophile
Mar 08 2010
prev sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Nicolas:
 The first error looks like a bug.
 The second one: I don't know if the template deduction engine is supposed to
resolve this.
For now I have added the second only: http://d.puremagic.com/issues/show_bug.cgi?id=3904 Bye, bearophile
Mar 08 2010
parent biozic <dransic free.fr> writes:
Le 09/03/10 00:28, bearophile a écrit :
 Nicolas:
 The first error looks like a bug.
 The second one: I don't know if the template deduction engine is supposed to
resolve this.
For now I have added the second only: http://d.puremagic.com/issues/show_bug.cgi?id=3904
Thanks. I've filed the first one, just in case. Bye, Nicolas
Mar 09 2010