tcstring.nim 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. discard """
  2. targets: "c cpp js"
  3. """
  4. type Result = enum none, a, b, c, d, e, f
  5. proc foo1(x: cstring): Result =
  6. const y = cstring"hash"
  7. const arr = [cstring"it", cstring"finally"]
  8. result = none
  9. case x
  10. of "Andreas", "Rumpf": result = a
  11. of cstring"aa", "bb": result = b
  12. of "cc", y, "when": result = c
  13. of "will", arr, "be", "generated": result = d
  14. of nil: result = f
  15. var results = [
  16. foo1("Rumpf"), foo1("Andreas"),
  17. foo1("aa"), foo1(cstring"bb"),
  18. foo1("cc"), foo1("hash"),
  19. foo1("finally"), foo1("generated"),
  20. foo1("no"), foo1("another no"),
  21. foo1(nil)]
  22. doAssert results == [a, a, b, b, c, c, d, d, none, none, f], $results
  23. proc foo2(x: cstring): Result =
  24. const y = cstring"hash"
  25. const arr = [cstring"it", cstring"finally"]
  26. doAssert not (compiles do:
  27. result = case x
  28. of "Andreas", "Rumpf": a
  29. of cstring"aa", "bb": b
  30. of "cc", y, "when": c
  31. of "will", arr, "be", "generated": d)
  32. case x
  33. of "Andreas", "Rumpf": a
  34. of cstring"aa", "bb": b
  35. of "cc", y, "when": c
  36. of "will", arr, "be", "generated": d
  37. of nil: f
  38. else: e
  39. results = [
  40. foo2("Rumpf"), foo2("Andreas"),
  41. foo2("aa"), foo2(cstring"bb"),
  42. foo2("cc"), foo2("hash"),
  43. foo2("finally"), foo2("generated"),
  44. foo2("no"), foo2("another no"),
  45. foo2(nil)]
  46. doAssert results == [a, a, b, b, c, c, d, d, e, e, f], $results