-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Feature request for a NimNode to typedesc conversion proc in macros #6785
Comments
similar with above issue, inside macro/ for example: type
Fruit = enum
apple
banana
orange
macro hello(x: static[Fruit]) =
echo x # here x is a compile time value
macro foo(x: typed) =
# x is a symbol
echo "possible but not simple"
macro bar(x: untyped) =
# x is a list of idents
echo "possible but not simple"
hello(apple) # ok
foo(apple)
bar:
apple
banana
orange we have static param and typedesc param for macro, we can convert NimNode into runtime literal. |
The issue as it is proposed cannot work. Let's assume there would be a proc f(T: typedesc[SomeInteger]) =
echo "called f with some integer"
proc f(T: typedesc[string]) =
echo "called f with string"
static:
let xxx = [int, string] # xxx is now such a typedesc
echo f(xxx[0]) nimout:
You have to generate the call to import macros
proc f(T: typedesc[SomeInteger]) =
echo "called f with some integer"
proc f(T: typedesc[string]) =
echo "called f with string"
proc f(arg: NimNode) =
let someInt = bindSym"SomeInteger"
echo someInt.getImpl.treeREpr
#echo arg.treeRepr
macro aMacro(x: typed) =
result = quote do:
f(typeof(`x`))
aMacro(1) # called f with some integer
aMacro("string") # called f with string
The overall conclusion is, So I have to reject this proposal as it does not solve anything. @jangko I just made a PR, so that you can get enums values from symbols easily. I hope |
I don't get what your example has to do with the original example. In the original example all necessary information should be in the |
Please elaborate on what you don't understand. I went into quite some detail about why your proposal can't work. |
@krux02 thank you for the enum magic, that would be very useful. @bluenote10 : I think from @krux02 example, he want to says static:
let xxx = [int, string] # xxx is now such a typedesc
echo f(xxx[0]) but your example also a bit confusing, I think if it modified a bit, it would make your intention clearer. import macros
proc f(T: typedesc[SomeInteger]) =
echo "called f with some integer"
proc f(T: typedesc[string]) =
echo "called f with string"
macro aMacro(x: typed) =
let typeNode = x.getType()
const T = typeMagic(typeNode) # `typeMagic` is not available in macros module
f(T)
aMacro(1)
aMacro("string") |
It's certainly possible to define semantics that will make @krux02's example work. |
@krux02 The use case is to call Your work-around doesn't address the use case, which is to call |
Sorry, but no. procs remain procs with static dispatich. And methods remain methods with dynamic dispatch. This is out of discussion. |
If Currently I can't use the I.e. another case of nim-lang/RFCs#44 |
- includes type system workaround: generic sandwich nim-lang/Nim#11225 - converting NimNode to typedesc: nim-lang/Nim#6785
* Implement a Sage codegenerator for frobenius constants * Sage codegen for pairings * Autogen of endomorphism acceleration constants * The autogen fixed a copy-paste bug in lattice decomposition. We can use conditional negation now and save an add+dbl in scalar mul * small fixes * sage code for square root bls12-377 is not old * readme updates * Provide test suggestions for derive_frobenius * indentation + add equation form to sage * Sage test vector generator * Use the json vectors - includes type system workaround: generic sandwich nim-lang/Nim#11225 - converting NimNode to typedesc: nim-lang/Nim#6785 * Delete old sage code * Install nim-serialization and nim-json-serialization in CI * CI nimble install force yes
Another: https://forum.nim-lang.org/t/8913 |
Currently,
macros.nim
does not offer a way to convert aNimNode
(obtained fromgetType
on a typed expression) into the corresponding typedesc:Error:
Note that there is a workaround: Doubling the argument of the macro into
aMacro(x: type, typedescOfX: typedesc)
, and one could write a wrapper macro that takes a typed expressionx
and generates the ASTaMacro(x, type(x))
.The text was updated successfully, but these errors were encountered: