twith.nim 746 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. discard """
  2. matrix: "--mm:refc; --mm:orc"
  3. """
  4. import std/with
  5. import std/[assertions, formatfloat]
  6. type
  7. Foo = object
  8. col, pos: string
  9. name: string
  10. proc setColor(f: var Foo; r, g, b: int) = f.col = $(r, g, b)
  11. proc setPosition(f: var Foo; x, y: float) = f.pos = $(x, y)
  12. var f: Foo
  13. with(f, setColor(2, 3, 4), setPosition(0.0, 1.0))
  14. doAssert f.col == "(2, 3, 4)"
  15. doAssert f.pos == "(0.0, 1.0)"
  16. f = Foo()
  17. with f:
  18. col = $(2, 3, 4)
  19. pos = $(0.0, 1.0)
  20. _.name = "bar"
  21. doAssert f.col == "(2, 3, 4)"
  22. doAssert f.pos == "(0.0, 1.0)"
  23. doAssert f.name == "bar"
  24. type
  25. Baz* = object
  26. a*, b*: int
  27. Bar* = object
  28. x*: int
  29. baz*: Baz
  30. var bar: Bar
  31. with bar:
  32. x = 1
  33. with baz:
  34. a = 2
  35. doAssert bar.x == 1
  36. doAssert bar.baz.a == 2