-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RFC] Working with types in macro is difficult. #44
Comments
Not sure if a bug or expected but the following getAST gives import macros, typetraits
proc getSubType(T: NimNode): NimNode =
echo getTypeInst(T).treerepr
result = getTypeInst(T)[1]
macro typed_helper(x: typed): untyped =
result = getSubType(x)
macro untyped_heavylifting(x: varargs[untyped]): untyped =
let first = x[0]
# result = quote do: # Works
# typed_helper(`first`)
result = getAST(typed_helper(first)) # Node has no type
var a: seq[int]
echo untyped_heavylifting(a) |
result = getAST(typed_helper(first.type)) |
That gives typedesc[NimNode] |
getAST(typed_helper(first.type))[0]? |
@CodeDoes first.type is of type NimNode because it's an AST node representing x, it is not x. You need to use one of the getType, getTypeInst or getTypeImpl to access the type of x. |
The linked #8531 is another example of the pitfalls of |
I don't completely understand everything in this post, but I do know that trying to use To get my code working, I used a modified version of proc replaceNodes*(ast: NimNode): NimNode =
# Replace NimIdent and NimSym by a fresh ident node
proc inspect(node: NimNode): NimNode =
case node.kind:
of {nnkIdent, nnkSym}:
return ident($node)
of nnkEmpty:
return node
of nnkLiterals:
return node
of nnkOpenSymChoice:
return inspect(node[0])
else:
var rTree = node.kind.newTree()
for child in node:
rTree.add inspect(child)
return rTree
result = inspect(ast) For reference, here's my original problem: https://forum.nim-lang.org/t/4300#26770 |
Tests that don't involve serialization are passing, tests that involve serialization are broken. Example command: `nim c -r --hints:off --warnings:off --verbosity:0 --outdir:build tests/official/test_fixture_operations_attester_slashings.nim`
* Introduce Fr type: finite field over curve order. Need workaround for nim-lang/Nim#16774 * Split curve properties into core and derived * Attach field properties to an instantiated field instead of the curve enum * Workaround nim-lang/Nim#14021, yet another "working with types in macros" is difficult nim-lang/RFCs#44 * Implement finite field over prime order of a curve subgroup * skip OpenSSL tests on windows
Another: https://forum.nim-lang.org/t/8913 |
Context
I find working with types in macro quite difficult and I think Nim would gain a lot if it was easier.
The 2 main issues I've noted are:
getAST
/quote do
as we can't call a macro from a macro.Cannot instantiate
ortype mismatch
because of forgetting to convert the NimSym given bygetTypeInst
toNimIdent
if they are not used raw.Example:
The text was updated successfully, but these errors were encountered: