typesapi2.nim 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # tests to see if a symbol returned from macros.getType() can
  2. # be used as a type
  3. import macros
  4. macro testTypesym (t:typed): untyped =
  5. var ty = t.getType
  6. if ty.typekind == ntyTypedesc:
  7. # skip typedesc get to the real type
  8. ty = ty[1].getType
  9. if ty.kind == nnkSym: return ty
  10. assert ty.kind == nnkBracketExpr
  11. assert ty[0].kind == nnkSym
  12. result = ty[0]
  13. return
  14. type TestFN = proc(a,b:int):int
  15. var iii: testTypesym(TestFN)
  16. static: assert iii is TestFN
  17. proc foo11 : testTypesym(void) =
  18. echo "HI!"
  19. static: assert foo11 is (proc():void {.nimcall.})
  20. var sss: testTypesym(seq[int])
  21. static: assert sss is seq[int]
  22. # very nice :>
  23. static: assert array[2,int] is testTypesym(array[2,int])
  24. static: assert(ref int is testTypesym(ref int))
  25. static: assert(void is testTypesym(void))
  26. macro tts2 (t:typed, idx:int): untyped =
  27. var ty = t.getType
  28. if ty.typekind == ntyTypedesc:
  29. # skip typedesc get to the real type
  30. ty = ty[1].getType
  31. if ty.kind == nnkSym: return ty
  32. assert ty.kind == nnkBracketExpr
  33. return ty[idx.intval.int]
  34. type TestFN2 = proc(a:int,b:float):string
  35. static:
  36. assert(tts2(TestFN2, 0) is TestFN2)
  37. assert(tts2(TestFN2, 1) is string)
  38. assert(tts2(TestFN2, 2) is int)
  39. assert(tts2(TestFN2, 3) is float)