tconceptinclosure.nim 675 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. discard """
  2. output: '''
  3. 10
  4. 20
  5. int
  6. 20
  7. 3
  8. '''
  9. """
  10. import typetraits
  11. type
  12. FonConcept = concept x
  13. x.x is int
  14. GenericConcept[T] = concept x
  15. x.x is T
  16. const L = T.name.len
  17. Implementation = object
  18. x: int
  19. Closure = object
  20. f: proc()
  21. proc f1(x: FonConcept): Closure =
  22. result.f = proc () =
  23. echo x.x
  24. proc f2(x: GenericConcept): Closure =
  25. result.f = proc () =
  26. echo x.x
  27. echo GenericConcept.T.name
  28. proc f3[T](x: GenericConcept[T]): Closure =
  29. result.f = proc () =
  30. echo x.x
  31. echo x.L
  32. let x = Implementation(x: 10)
  33. let y = Implementation(x: 20)
  34. let a = x.f1
  35. let b = x.f2
  36. let c = x.f1
  37. let d = y.f2
  38. let e = y.f3
  39. a.f()
  40. d.f()
  41. e.f()