mirror of
https://github.com/bluenote10/oop_utils
synced 2026-01-14 09:31:41 +00:00
No description
|
|
||
|---|---|---|
| .github/workflows | ||
| benchmarks | ||
| dev | ||
| src/oop_utils | ||
| tests | ||
| .gitignore | ||
| .watchcode.yaml | ||
| nim.cfg | ||
| oop_utils.nimble | ||
| readme.md | ||
| readme_closure_class.md | ||
| readme_standard_class.md | ||
oop_utils

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