Last active
February 25, 2020 15:14
-
-
Save ba0f3/a8eb86c2f6ed5315e2e092b095ca031e to your computer and use it in GitHub Desktop.
assign default values for object's members
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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