
Working with Nim's macros is just Voodoo!
A collection of utilities to build awesome tools!
nimble install voodoo
Voodoo is a Nim package that provides a collection of tools and utilities to build awesome packages and applications using Nim's powerful macro system.
- Generate fast
getters/settersfrom object fields - Make
extensibleenums/objects
Here are some examples of what you can do with Voodoo.
Generate fast getters from object fields without explicitly writing them. Currently, in Nim you cannot read private fields from other modules. Voodoo's getters pragma makes it easy to generate public getters for private fields.
Excluding specfic fields is also supported.
import pkg/voodoo
type
Price* {.getters.} = object
net, gross: string
Product* {.getters: [id].} = object # exclude one or more fields
id: string
title, short_description: string
price: Price
expandGetters() # is required to expand generated procs.expandGetters will generate the following getters:
proc getNet*(price: Price): string =
## Get `net` from `Price`
result = price.net
proc getGross*(price: Price): string =
## Get `gross` from `Price`
result = price.gross
proc getTitle*(product: Product): string =
## Get `title` from `Product`
result = product.title
proc getShortDescription*(product: Product): string =
## Get `short_description` from `Product`
result = product.short_description
proc getPrices*(product: Product): Price =
## Get `price` from `Product`
result = product.pricetodo
It's easy to make extensible enums/objects. This is super useful when building frameworks or libraries where users may want to extend your types.
Also, extensible pragma works with both public or private definitions
import voodoo/extensible
type
Cardinal* {.extensible} = enum
north, westDone! Now Cardinal is an extensible enum. Any other modules/packages importing it can easily add fields to this enum. Yep, that's voodoo!
import voodoo/extensible
# `extendEnum` macro is used to add new fields to an extensible enum.
# it is required to do this before importing the extensible enum's module.
extendEnum Cardinal:
south
east
# the extensible enum's module is imported after we setup the extensions.
import ./cardinalModule
assert compiles(Cardinal.north)
assert compiles(Cardinal.south)
assert compiles(Cardinal.east)- π Found a bug? Create a new Issue
- π Wanna help? Fork it!
- π Get β¬20 in cloud credits from Hetzner
MIT license. Made by Humans from OpenPeeps.
Copyright Β© OpenPeeps & Contributors β All rights reserved.