Skip to content

Latest commit

Β 

History

History
113 lines (84 loc) Β· 3.35 KB

File metadata and controls

113 lines (84 loc) Β· 3.35 KB


Working with Nim's macros is just Voodoo!
A collection of utilities to build awesome tools!

nimble install voodoo

About

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.

API reference
Github Actions Github Actions

😍 Key Features

  • Generate fast getters/setters from object fields
  • Make extensible enums/objects

Examples

Here are some examples of what you can do with Voodoo.

Getters

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.price

Setters

todo

Extensibles

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, west

Done! 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)

❀ Contributions & Support

🎩 License

MIT license. Made by Humans from OpenPeeps.
Copyright Β© OpenPeeps & Contributors β€” All rights reserved.