twhen1.nim 759 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. const Z = 0
  2. type
  3. Foo[T] = object
  4. when true:
  5. u: int
  6. else:
  7. v: int
  8. Foo1[T] = object
  9. when T is int:
  10. x: T
  11. elif true:
  12. z: char
  13. Foo2[x:static[int]] = object
  14. when (x and 1) == 1:
  15. x: array[x+1,int]
  16. else:
  17. x: array[x,int]
  18. Foo3 = Foo2[128]
  19. # #8417
  20. Foo4[A: static[int]] = object
  21. when Z == 0:
  22. discard
  23. else:
  24. discard
  25. block:
  26. var x: Foo[int] = Foo[int](u: 42)
  27. doAssert x.u == 42
  28. # Don't evaluate `when` branches before the type is instantiated
  29. block:
  30. var x: Foo1[bool] = Foo1[bool](z: 'o')
  31. doAssert x.z == 'o'
  32. block:
  33. var x: Foo2[3]
  34. doAssert x.x.len == 4
  35. block:
  36. var x: Foo2[4]
  37. doAssert x.x.len == 4
  38. block:
  39. var x: Foo3
  40. doAssert x.x.len == 128
  41. block:
  42. var x: Foo4[0]