tmetatype_various.nim 1.3 KB

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