Advertisement
Guest User

Untitled

a guest
Aug 30th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. void main(){
  2. import std.stdio;
  3. auto nodeI = new Node!(int)([1,2,3]);
  4. auto nodeF = new Node!(float)([1f,2f,3f]); /+ fails with error:
  5. $ dmd test.d
  6. test.d(26): Error: function literal __lambda4 (int t) is not callable using argument types (float)
  7. /Library/D/dmd/src/phobos/std/algorithm/iteration.d(480): instantiated from here: MapResult!(__lambda1, float[])
  8. test.d(26): instantiated from here: map!(float[])
  9. test.d(4): instantiated from here: Node!(float, __lambda4)
  10.  
  11. +/
  12.  
  13. auto nodeF2 = new Node!(float, (float f) => f*f)([1f,2f,3f]); // works
  14. writeln(nodeI.test);
  15. writeln(nodeF.test);
  16. }
  17.  
  18. template Node(T, alias func=(T t) => t*t){
  19. // Predefining the alias function seems to cause hassle with nodeF above
  20. class Node{
  21. T[] items;
  22. Node[] children;
  23.  
  24. this(T[] ts){
  25. items = ts[];
  26. }
  27.  
  28. static if(func is null){
  29. auto test(){
  30. return items;
  31. }
  32. }
  33. static if(func !is null){
  34. auto test(){
  35. import std.algorithm;
  36. return items.map!(a => func(a));
  37. }
  38. }
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement