-
-
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
Typedesc forwarding: macros lose typedesc information #6784
Comments
I don't think this is a bug. |
This issue has been fixed differently. The error message is now the following: /tmp/scratch.nim(13, 4) Error: type mismatch: got <NimNode>
but expected one of:
proc f(T: typedesc[SomeInteger])
first type mismatch at position: 1
required type for T: typedesc[SomeInteger]
but expression 'T' is of type: NimNode
proc f(T: typedesc[string])
first type mismatch at position: 1
required type for T: typedesc[string]
but expression 'T' is of type: NimNode As typedesc arguments are now passed in as |
Hm, and how can we make such code work? The code looks it should compile... The fix has turned the problem into #6785 which I still don't understand why it had to be closed without a solution. It would be nice to extend the documentation why such code is conceptionally impossibly and how to work-around the issue. |
I think what you want to solve your problem is a macro API for type relations, the macro aMacro(T: typedesc) =
let typ = getTypeInst(T)[1]
if sameType(typ, bindSym"SomeInteger"):
echo "type is SomeInteger" # never called
if sameType(typ, bindSym"string"):
echo "type is string" If that is what you want, please make a separate feature request for it. The title issues |
Not quite, the use case was to call any existing function that takes a typedesc argument from within a macro. I run into this issue a lot, I design a macro to take a typedesc argument and at some point I need to call an existing typedesc function from the macro world, mainly standard library stuff like:
After the conversion to NimNode the argument is no longer usable as a typedesc, which was what I meant by "loses typedesc information". |
For |
I think there is no problem here, sorry. In the macro you have NimNodes, you can inspect the type via the |
I have explained the original vision for Indeed, one of my goals was to make all Certain type traits might be defined as templates. Consider this example: template ElemType*[T](x: typedesc[openarray[T]]): untyped = T
template ElemType*(x: typedesc[string]): untyped = char A perfect compiler will allow me to use such templates inside a macro in the following way: macro foo(T: typedesc): untyped =
var E = ElemType(T)
echo E.treeRepr This can work if the compiler is able to automatically insert This is controversial, because resolving overloads depends on the current scope, so we'll have to define that macros are evaluated in a particular scope. For me, this is a good development, because it will eventually allow us to introduce magics such as The original design was also handling the |
That is a good idea in general. |
Why not? I can't remember my original use cases but they all had to do with moving work from runtime to compile time. In the But having done some more experiments I see that this is simply not how generic macros work. |
You should do this at initialization time of the program. Anything else will bloat your executable with unnecessary data and it will kill your compilation time as not only do your random numbers need to be calculated by the NimVm which is already not very fast, they also need to be hauled through the C compiler. Apart from this, I don't see how this is related to typedesc. , it would be a reasonable optimization to directly produce an AST (which becomes more significant when the work gets non-trivial like using rejection sampling). This particular example can probably be solved by
|
Forwarding a typedesc works in general for procs, i.e., if a proc has a broad typedesc argument
T: typedesc
it can call narrower procs that have a specificT: typedesc[<some-type>]
argument. The same does not work for macros:Error:
The text was updated successfully, but these errors were encountered: