Source Edit

This module implements the with macro for easy function chaining. See https://github.com/nim-lang/RFCs/issues/193 and https://github.com/nim-lang/RFCs/issues/192 for details leading to this particular design.

Since: version 1.2.

Imports

macros, underscored_calls

Macros

  1. macro with(arg: typed; calls: varargs[untyped]): untyped

This macro provides chaining of function calls. It does so by patching every call in calls to use arg as the first argument.

Caution: This evaluates arg multiple times!

Example:

  1. var x = "yay"
  2. with x:
  3. add "abc"
  4. add "efg"
  5. doAssert x == "yayabcefg"
  6. var a = 44
  7. with a:
  8. += 4
  9. -= 5
  10. doAssert a == 43
  11. # Nesting works for object types too!
  12. var foo = (bar: 1, qux: (baz: 2))
  13. with foo:
  14. bar = 2
  15. with qux:
  16. baz = 3
  17. doAssert foo.bar == 2
  18. doAssert foo.qux.baz == 3

Source Edit