Skip to content

Latest commit

 

History

History
102 lines (80 loc) · 2.83 KB

README.md

File metadata and controls

102 lines (80 loc) · 2.83 KB


Working with Nim's macros 👑 is just Voodoo

nimble install voodoo

API reference
Github Actions Github Actions

😍 Key Features

  • Compile-time utility tool
  • Helps reduce code size & repetitive tasks
  • Generate fast getters/setters
  • Callee Introspection

Examples

Getters

Generate fast getters from object fields. Excluding fields is also possible.

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.

Output:

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 may allow other modules/packages to extend the functionality as if they were written in the original source.

Also, extensible pragma works with both public or private definitions. Well, that looks like Voodoo to me!

import pkg/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 pkg/voodoo/extensible
extendEnum Cardinal:
  south
  east

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 © 2024 OpenPeeps & Contributors — All rights reserved.