twith.nim 481 B

123456789101112131415161718192021222324
  1. import std/with
  2. type
  3. Foo = object
  4. col, pos: string
  5. name: string
  6. proc setColor(f: var Foo; r, g, b: int) = f.col = $(r, g, b)
  7. proc setPosition(f: var Foo; x, y: float) = f.pos = $(x, y)
  8. var f: Foo
  9. with(f, setColor(2, 3, 4), setPosition(0.0, 1.0))
  10. doAssert f.col == "(2, 3, 4)"
  11. doAssert f.pos == "(0.0, 1.0)"
  12. f = Foo()
  13. with f:
  14. col = $(2, 3, 4)
  15. pos = $(0.0, 1.0)
  16. _.name = "bar"
  17. doAssert f.col == "(2, 3, 4)"
  18. doAssert f.pos == "(0.0, 1.0)"
  19. doAssert f.name == "bar"