Skip to content

Instantly share code, notes, and snippets.

@mratsim
Created April 6, 2018 16:25
Show Gist options
  • Select an option

  • Save mratsim/c9ac14d049222d0c6bce6daa1bfd8fad to your computer and use it in GitHub Desktop.

Select an option

Save mratsim/c9ac14d049222d0c6bce6daa1bfd8fad to your computer and use it in GitHub Desktop.
template defineIntConstructor(typ: typedesc, name: untyped{nkIdent}) =
template name*(a: int64): typ = initInt[typ](a)
template name*(a: cstring): typ = initInt[typ](a)
template `+`*(a: typ, b: int{lit}): typ = a + initInt[typ](b)
template `+`*(a: int{lit}, b: typ): typ = initInt[typ](a) + b
template `-`*(a: typ, b: int{lit}): typ = a - initInt[typ](b)
template `-`*(a: int{lit}, b: typ): typ = initInt[typ](a) - b
defineIntConstructor(Int256, i256)
defineIntConstructor(Int512, i512)
defineIntConstructor(Int1024, i1024)
defineIntConstructor(Int2048, i2048)
template defineUIntConstructor(typ: typedesc, name: untyped{nkIdent}) =
template name*(a: uint64): typ = initUInt[typ](a)
template name*(a: cstring): typ = initInt[typ](a)
template `+`*(a: typ, b: int{lit}): typ = a + initUInt[typ](b)
template `+`*(a: int{lit}, b: typ): typ = initUInt[typ](a) + b
template `-`*(a: typ, b: int{lit}): typ = a - initUInt[typ](b)
template `-`*(a: int{lit}, b: typ): typ = initUInt[typ](a) - b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment