Skip to content

Feature request for a NimNode to typedesc conversion proc in macros #6785

@bluenote10

Description

@bluenote10

Currently, macros.nim does not offer a way to convert a NimNode (obtained from getType on a typed expression) into the corresponding typedesc:

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()
  # Call f at compile time is not possible because
  # typeNode can't be converted to typedesc.
  f(typeNode)

aMacro(1)
aMacro("string")

Error:

test.nim(13, 4) Error: type mismatch: got (NimNode)
but expected one of: 
proc f(T: typedesc[SomeInteger])
proc f(T: typedesc[string])

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 expression x and generates the AST aMacro(x, type(x)).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions