Hello, I'm very new with nim language. it looks really nice. one thing I've found is that its macro syntax is really powerful. that made me wonder: would it be possible to mimic kotlin's scope functions ? there's five variants: let, with, run, apply, and also, and they help with cases such as object initialization and grouping function calls.
I'll try implementing them myself, but any help would be greatly appreciated.
For some existing packages that may cover some of the same functionality
There is no "class scope" in Nim like in Java and Kotlin, but you can mimic it by declaring equivalent routines inside a new scope:
type Foo = object
x: int
proc incr(foo: var Foo) = inc foo.x
template withFoo(body) =
block: # new scope
var foo = Foo()
template incr() = incr(foo)
template x: var int = foo.x
body
withFoo:
echo x # 0
x = 3
echo x # 3
incr()
echo x # 4
You can automatically generate these sometimes, for example you can generate templates similar to x for all of an object type's fields. There are packages that do this already. But you won't be able to generate wrappers for every routine in scope that has the object type as the first parameter, though this shouldn't matter for most use cases anyway.