Skip to content

Instantly share code, notes, and snippets.

@fowlmouth
Created March 26, 2015 04:17
Show Gist options
  • Save fowlmouth/dd7f4f31e44cfc6cc042 to your computer and use it in GitHub Desktop.
Save fowlmouth/dd7f4f31e44cfc6cc042 to your computer and use it in GitHub Desktop.
import macros
proc concreteType* (n:NimNode): NimNode{.compileTime.}=
return case n.typeKind
of ntyTypeDesc:
n[1].concreteType
else:
n
macro implementing (f:stmt, body:stmt): expr =
let ty = f.getType.concreteType
assert ty.typeKind == ntyProc
assert body.kind == nnkDo
# return a function implementing `f`
var params: seq[NimNode] = @[]
for i in 1 .. high(body.params):
let p = body.params[i]
for ii in 0 .. len(p)-3:
params.add p[ii]
result = newProc(procKind = nnkLambda
echo body.typekind
echo body.treerepr
echo ty.typekind
echo ty.treerepr
import typetraits
type
TFunc = proc(a,b: int): int
discard(implementing(TFunc) do (a,b):
return a+b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment