tmetatype_various.nim 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. discard """
  2. matrix: "--mm:refc; --mm:refc"
  3. output: '''[1, 0, 0, 0, 0, 0, 0, 0] CTBool[Ct[system.uint32]]'''
  4. """
  5. block tconstraints:
  6. proc myGenericProc[T: object|tuple|int|ptr|ref|distinct](x: T): string =
  7. result = $x
  8. type TMyObj = tuple[x, y: int]
  9. var x: TMyObj
  10. assert myGenericProc(232) == "232"
  11. assert myGenericProc(x) == "(x: 0, y: 0)"
  12. block tfieldaccessor:
  13. type
  14. Test = object
  15. x: int
  16. case p: bool
  17. of true:
  18. a: int
  19. else:
  20. case q: bool
  21. of true:
  22. b: int
  23. else:
  24. discard
  25. proc f[T](t: typedesc[T]): int =
  26. 1
  27. assert Test.f == 1
  28. block tprocbothmeta:
  29. proc myFun[A](x: A): auto =
  30. result = float(x+10)
  31. proc myMap[T,S](sIn: seq[T], f: proc (q: T): S): seq[S] =
  32. result = newSeq[S](sIn.len)
  33. for i in 0..<sIn.len:
  34. result[i] = f(sIn[i])
  35. assert myMap(@[1,2,3], myFun) == @[11.0, 12.0, 13.0]
  36. # https://github.com/nim-lang/Nim/issues/13646
  37. type
  38. BaseUint* = SomeUnsignedInt or byte
  39. Ct*[T] = distinct T
  40. ## Constant-Time wrapper
  41. ## Only constant-time operations in particular the ternary operator equivalent
  42. ## condition: if true: a else: b
  43. ## are allowed
  44. CTBool*[T] = distinct range[T(0)..T(1)]
  45. ## Constant-Time boolean wrapper
  46. var x: array[8, CTBool[Ct[uint32]]]
  47. x[0] = (CTBool[Ct[uint32]])(1)
  48. echo x.repr, " ", typeof(x[0])