Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bluenote10/oop_utils
Nim macros for building OOP class hierarchies.
https://github.com/bluenote10/oop_utils
Last synced: 30 days ago
JSON representation
Nim macros for building OOP class hierarchies.
- Host: GitHub
- URL: https://github.com/bluenote10/oop_utils
- Owner: bluenote10
- Created: 2019-03-24T17:52:21.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2021-06-09T08:05:39.000Z (over 3 years ago)
- Last Synced: 2024-11-07T02:30:47.838Z (3 months ago)
- Language: Nim
- Homepage:
- Size: 101 KB
- Stars: 35
- Watchers: 5
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
- awesome-nim - oop_utils - Nim macros for building OOP class hierarchies. (Language Features / Object-Oriented Programming)
README
# oop_utils [![Build Status](https://github.com/bluenote10/oop_utils/workflows/ci/badge.svg)](https://github.com/bluenote10/oop_utils/actions?query=workflow%3Aci) [![license](https://img.shields.io/github/license/mashape/apistatus.svg)](LICENSE)
oop_utils provides macros that allow to easily create OOP class hierarchies.
It comes in two different flavors:
- [Standard classes](readme_standard_class.md): Allows to define type hierarchies based on Nim's standard method dispatch.
- [Closure classes](readme_closure_class.md): 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:
```nim
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 = initmethod inc*() {.base.} = self.counter.inc
method dec*() {.base.} = self.counter.dec
method get*(): int {.base.} = self.counter
```vs.
```nim
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
```