t11637.nim 965 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. type Foo = ref object
  2. val: int
  3. proc `+`(a, b: Foo): Foo =
  4. Foo(val: a.val + b.val)
  5. proc `*`(a: Foo, b: int): Foo =
  6. Foo(val: a.val * b)
  7. proc `+=`(a: var Foo, b: Foo) =
  8. a = Foo(
  9. val: a.val + b.val
  10. )
  11. proc foobar(a, b, c: Foo): tuple[bar, baz, buzz: Foo] =
  12. let foo = a + b + c
  13. result.bar = foo * 2
  14. result.baz = foo * 3
  15. result.buzz = result.baz
  16. result.buzz += a * 10000
  17. result.baz += b
  18. result.buzz += b
  19. block: # Compile-Time
  20. let
  21. a {.compileTime.} = Foo(val: 1)
  22. b {.compileTime.} = Foo(val: 2)
  23. c {.compileTime.} = Foo(val: 3)
  24. r {.compileTime.} = foobar(a, b, c)
  25. static:
  26. doAssert r.bar.val == 12
  27. doAssert r.baz.val == 20
  28. doAssert r.buzz.val == 10020
  29. ####################################
  30. block: # Run-time
  31. let
  32. a = Foo(val: 1)
  33. b = Foo(val: 2)
  34. c = Foo(val: 3)
  35. r = foobar(a, b, c)
  36. # Expected values
  37. doAssert r.bar.val == 12
  38. doAssert r.baz.val == 20
  39. doAssert r.buzz.val == 10020