I didn't know that this can be solved by forwarding from one macro to another,
will try that, looks really nice.
I will experiment a bit more regarding the field accessors when I'm back from
vacation. Overall this has a bit lower priority to me, because the use cases I
have barely require
> Counter of SomeBaseClass would require an untyped argument, and I don't think
> it would be possible to access the type impl in this case.
Would this help? It generates a call to a macro which takes the base class type
as a typed arg:
import macros
type
B = object
Another way around this is overloading dot operators. Apparently this now works
without the `{.experimental: "dotOperators".}` pragma:
type
Obj = ref object
getStep: proc(): int
setStep: proc(x: int)
template `.`(o: Obj, f: untyped): untyped = (o.`get
for property access you can use a distinct type and for assignment an operator
like :=
type
Obj = ref object
fStep: int
ObjStepProperty = distinct Obj
template step(self: Obj): ObjStepProperty = self.ObjStepProperty
proc `:=` (self:
@gemath That's also something I had in mind ;).
The problem with autogenerating getters/setters for `var x*` expressions is
that closures don't allow the regular `field` / `field=` syntax as far as I can
see. For instance:
type
Obj = ref object
step: proc(): int
Nice work, this closure-as-an-object pattern is quite useful in JavaScript as
an alternative to its IMHO convoluted prototype-based OO.
That being said, a syntax which retains the meaning of `*` and is more compact
might be preferable, something more like Scala's classes perhaps:
So far I only ran some stupid benchmarks on dispatch speed
[here](https://github.com/bluenote10/closure_methods/blob/master/benchmarks/basic_dispatch_closures.nim).
and in that terms using closures is a bit faster than the branch based
dispatch of methods, even if the number of subclasses is
I'm concerned about the overhead. Are there any benchmarks?
Yet another OOP macro ;). [*]
I was wondering if it would be possible to write classes in Nim entirely by
using closures. A good opportunity to work on my meta programming skills. At
first the approach looked a bit weird, but now I feel that the resulting DSL
might actually work quite well.