Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. fn S(comptime T: type) type {
  2. return struct {
  3. const Self = @This();
  4.  
  5. foo: fn (a: T, b: T) bool,
  6.  
  7. fn init(foo: fn (a: T, b: T) bool) Self {
  8. return Self {
  9. .foo = foo
  10. };
  11. }
  12. };
  13. }
  14.  
  15. fn greaterThan1(a: u32, b: u32) bool {
  16. return a > b;
  17. }
  18.  
  19. fn greaterThan2(a: var, b: @typeOf(a)) bool {
  20. return a > b;
  21. }
  22.  
  23. fn greaterThan3(comptime T: type, a: T, b: T) bool {
  24. return a > b;
  25. }
  26.  
  27. // This works
  28. pub fn main() void {
  29. const s = S(u32).init(greaterThan1);
  30. warn("{}", s);
  31. }
  32.  
  33.  
  34. // ➜ deflate git:(master) ✗ zig build-exe priority_queue.zig && ./priority_queue
  35. // S(u32){ .foo = fn(u32, u32) bool@10c843850 }%
  36.  
  37.  
  38. // // No dice!
  39. pub fn main() void {
  40. const s = S(u32).init(greaterThan2);
  41. warn("{}", s);
  42. }
  43.  
  44.  
  45. // ➜ deflate git:(master) ✗ zig build-exe priority_queue.zig && ./priority_queue
  46. // /Users/john.schmidt/personal_programming/zig/deflate/priority_queue.zig:380:27: error: expected type 'fn(u32, u32) bool', found 'fn(var,var)var'
  47. // const s = S(u32).init(greaterThan2);
  48. // ^
  49. // /Users/john.schmidt/personal_programming/zig/deflate/priority_queue.zig:380:27: note: only one of the functions is generic
  50. // const s = S(u32).init(greaterThan2);
  51. ^
  52.  
  53. // Naturally, this doesn't work either
  54. pub fn main() void {
  55. const s = S(u32).init(greaterThan3);
  56. warn("{}", s);
  57. }
  58.  
  59. // ➜ deflate git:(master) ✗ zig build-exe priority_queue.zig && ./priority_queue
  60. // /Users/john.schmidt/personal_programming/zig/deflate/priority_queue.zig:380:27: error: expected type 'fn(u32, u32) bool', found 'fn(type,var,var)var'
  61. // const s = S(u32).init(greaterThan3);
  62. // ^
  63. // /Users/john.schmidt/personal_programming/zig/deflate/priority_queue.zig:380:27: note: only one of the functions is generic
  64. // const s = S(u32).init(greaterThan3);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement