Skip to content

Instantly share code, notes, and snippets.

@ba0f3
Last active February 25, 2020 15:14
Show Gist options
  • Save ba0f3/a8eb86c2f6ed5315e2e092b095ca031e to your computer and use it in GitHub Desktop.
Save ba0f3/a8eb86c2f6ed5315e2e092b095ca031e to your computer and use it in GitHub Desktop.
assign default values for object's members
import macros
template value(x: untyped) {.pragma.}
proc init[T](t: typedesc[T]): T =
for k, v in result.fieldPairs:
if v.hasCustomPragma(value):
v = v.getCustomPragmaVal(value)
type
MyObj = object
number {.value: 15}: int
hello {.value: "world".}: string
var o = init MyObj
assert o.number == 15
assert o.hello == "world"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment