oop_utils provides macros that allow to easily create OOP class hierarchies.
It comes in two different flavors:
- Standard classes: Allows to define type hierarchies based on Nim's standard method dispatch.
- Closure classes: Allows to define type hierarchies based on closure method dispatch.
The two approaches have minor syntactical differences, but share the same general scheme. For comparison:
import oop_utils/standard_class
# A standard class:
# - The object instance is attached to a `self` symbol.
# - Initialization + field definitions go into the ctor proc.
class(Counter):
ctor(newCounter) proc(init: int) =
self:
counter = init
method inc*() {.base.} = self.counter.inc
method dec*() {.base.} = self.counter.dec
method get*(): int {.base.} = self.counter
vs.
import oop_utils/closure_class
# A closure class:
# - No `self` symbol required.
# - Initialization + field definitions go into main scopre to emphasize closure nature.
class(Counter):
ctor(newCounter) proc(init: int)
var counter = init
proc inc*() = counter.inc
proc dec*() = counter.dec
proc get*(): int = counter