mirror of
https://github.com/nim-lang/Nim.git
synced 2026-02-18 17:08:32 +00:00
Merge pull request #2712 from nanoant/patch/macros-introduce-typenode-for-typedesc
Macros: Introduce typeNode(t: typedesc): NimNode
This commit is contained in:
@@ -177,6 +177,12 @@ proc getType*(n: NimNode): NimNode {.magic: "NGetType", noSideEffect.}
|
||||
## resolve recursive types, you have to call 'getType' again. To see what
|
||||
## kind of type it is, call `typeKind` on getType's result.
|
||||
|
||||
proc getType*(n: typedesc): NimNode {.magic: "NGetType", noSideEffect.}
|
||||
## Returns the Nim type node for given type. This can be used to turn macro
|
||||
## typedesc parameter into proper NimNode representing type, since typedesc
|
||||
## are an exception in macro calls - they are not mapped implicitly to
|
||||
## NimNode like any other arguments.
|
||||
|
||||
proc typeKind*(n: NimNode): NimTypeKind {.magic: "NGetType", noSideEffect.}
|
||||
## Returns the type kind of the node 'n' that should represent a type, that
|
||||
## means the node should have been obtained via `getType`.
|
||||
|
||||
20
tests/macros/tgettype.nim
Normal file
20
tests/macros/tgettype.nim
Normal file
@@ -0,0 +1,20 @@
|
||||
discard """
|
||||
msg: '''ObjectTy(Sym(Model), RecList(Sym(name), Sym(password)))
|
||||
BracketExpr(Sym(typeDesc), Sym(User))'''
|
||||
"""
|
||||
import strutils, macros
|
||||
|
||||
type
|
||||
Model = object of RootObj
|
||||
User = object of Model
|
||||
name : string
|
||||
password : string
|
||||
|
||||
macro testUser: expr =
|
||||
return newLit(User.getType.lispRepr)
|
||||
|
||||
macro testGeneric(T: typedesc[Model]): expr =
|
||||
return newLit(T.getType.lispRepr)
|
||||
|
||||
echo testUser
|
||||
echo User.testGeneric
|
||||
Reference in New Issue
Block a user