tmisc_issues.nim 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. discard """
  2. output: '''true
  3. true
  4. true
  5. true
  6. p has been called.
  7. p has been called.
  8. implicit generic
  9. generic
  10. false
  11. true
  12. -1'''
  13. """
  14. # https://github.com/nim-lang/Nim/issues/1147
  15. type TTest = object
  16. vals: seq[int]
  17. proc add*(self: var TTest, val: int) =
  18. self.vals.add(val)
  19. type CAddable = concept x
  20. x[].add(int)
  21. echo((ref TTest) is CAddable) # true
  22. # https://github.com/nim-lang/Nim/issues/1570
  23. type ConcretePointOfFloat = object
  24. x, y: float
  25. type ConcretePoint[Value] = object
  26. x, y: Value
  27. type AbstractPointOfFloat = generic p
  28. p.x is float and p.y is float
  29. let p1 = ConcretePointOfFloat(x: 0, y: 0)
  30. let p2 = ConcretePoint[float](x: 0, y: 0)
  31. echo p1 is AbstractPointOfFloat # true
  32. echo p2 is AbstractPointOfFloat # true
  33. echo p2.x is float and p2.y is float # true
  34. # https://github.com/nim-lang/Nim/issues/2018
  35. type ProtocolFollower = concept
  36. true # not a particularly involved protocol
  37. type ImplementorA = object
  38. type ImplementorB = object
  39. proc p[A: ProtocolFollower, B: ProtocolFollower](a: A, b: B) =
  40. echo "p has been called."
  41. p(ImplementorA(), ImplementorA())
  42. p(ImplementorA(), ImplementorB())
  43. # https://github.com/nim-lang/Nim/issues/2423
  44. proc put*[T](c: seq[T], x: T) = echo "generic"
  45. proc put*(c: seq) = echo "implicit generic"
  46. type
  47. Container[T] = concept c
  48. put(c)
  49. put(c, T)
  50. proc c1(x: Container) = echo "implicit generic"
  51. c1(@[1])
  52. proc c2[T](x: Container[T]) = echo "generic"
  53. c2(@[1])
  54. # https://github.com/nim-lang/Nim/issues/2882
  55. type
  56. Paper = object
  57. name: string
  58. Bendable = concept x
  59. bend(x is Bendable)
  60. proc bend(p: Paper): Paper = Paper(name: "bent-" & p.name)
  61. var paper = Paper(name: "red")
  62. echo paper is Bendable
  63. type
  64. A = concept self
  65. size(self) is int
  66. B = object
  67. proc size(self: B): int =
  68. return -1
  69. proc size(self: A): int =
  70. return 0
  71. let b = B()
  72. echo b is A
  73. echo b.size()