twith.nim 518 B

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