Created
March 26, 2015 04:17
-
-
Save fowlmouth/dd7f4f31e44cfc6cc042 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
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