Compiler extensions / module-range macros
I think Nim is almost there. One thing left is to implement pragma push for templates/macros.
import myModuleThatChangesEverything
{.push myPragmaThatChangesEverything.}
# rest of the code
{.pop.} # is it needed?
Type invariant inheritance
I would be really glad for type ct callbacks, that evaluate upon type use.
type MyMagicType = ref object of RootObj
macro `=onInherit`(t: typedesc[MyMagicType], n: untyped): untyped {.compileTime.} =
# do smth with n
result = n
macro `=onUsedAsArgument`(t: typedesc[MyMagicType], n: untyped): untyped {.compileTime.} =
# do smth with n
result = n
# Further usage
type MyInheritedType = ref object of MyMagicType
a: int
# This actually gets rewritten to:
# `=onInherit`(MyMagicType):
# type MyInheritedType = ref object of MyMagicType
# a: int
# Some routine:
proc someProcThatUsesMyType(a: int, v: MyMagicType) =
discard
# This actually gets rewritten to:
# `= onUsedAsArgument `(MyMagicType):
# proc someProcThatUsesMyType(a: int, v: MyMagicType) =
# discard
Further on the onUsedAsArgument can insert invariant calls to the proc. The onInherit can implement some code for the inherited type, or register it in a factory or whatever.
@Udiknedormin
My library on github: https://github.com/Udiknedormin/NimContracts
Could you add you library to the Nimble repository so that it will be discoverable by Nimble users?
It seems a nice extension for Nim.
Update:
Thanks to Nim now handling doc comments AST (it wasn't there back when I introduced the lib), contracts now generate docs. :)