From 2478be3f21c244ff67943a963d9c4bc0337cb437 Mon Sep 17 00:00:00 2001 From: jcosborn Date: Sun, 15 Apr 2018 06:51:16 -0500 Subject: [PATCH] add example to docs for getTypeInst and getTypeImpl (#7206) * add example to docs for getTypeInst and getTypeImpl * made examples use runnableExamples * changed assert to doAssert --- lib/core/macros.nim | 50 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/lib/core/macros.nim b/lib/core/macros.nim index e71b7cdc87..e76e9241f4 100644 --- a/lib/core/macros.nim +++ b/lib/core/macros.nim @@ -258,19 +258,55 @@ proc getType*(n: typedesc): NimNode {.magic: "NGetType", noSideEffect.} 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`. + ## means the node should have been obtained via ``getType``. -proc getTypeInst*(n: NimNode): NimNode {.magic: "NGetType", noSideEffect.} - ## Like getType except it includes generic parameters for a specific instance +proc getTypeInst*(n: NimNode): NimNode {.magic: "NGetType", noSideEffect.} = + ## Returns the `type`:idx: of a node in a form matching the way the + ## type instance was declared in the code. + runnableExamples: + type + Vec[N: static[int], T] = object + arr: array[N, T] + Vec4[T] = Vec[4, T] + Vec4f = Vec4[float32] + var a: Vec4f + var b: Vec4[float32] + var c: Vec[4, float32] + macro dumpTypeInst(x: typed): untyped = + newLit(x.getTypeInst.repr) + doAssert(dumpTypeInst(a) == "Vec4f") + doAssert(dumpTypeInst(b) == "Vec4[float32]") + doAssert(dumpTypeInst(c) == "Vec[4, float32]") proc getTypeInst*(n: typedesc): NimNode {.magic: "NGetType", noSideEffect.} - ## Like getType except it includes generic parameters for a specific instance + ## Version of ``getTypeInst`` which takes a ``typedesc``. -proc getTypeImpl*(n: NimNode): NimNode {.magic: "NGetType", noSideEffect.} - ## Like getType except it includes generic parameters for the implementation +proc getTypeImpl*(n: NimNode): NimNode {.magic: "NGetType", noSideEffect.} = + ## Returns the `type`:idx: of a node in a form matching the implementation + ## of the type. Any intermediate aliases are expanded to arrive at the final + ## type implementation. You can instead use ``getImpl`` on a symbol if you + ## want to find the intermediate aliases. + runnableExamples: + type + Vec[N: static[int], T] = object + arr: array[N, T] + Vec4[T] = Vec[4, T] + Vec4f = Vec4[float32] + var a: Vec4f + var b: Vec4[float32] + var c: Vec[4, float32] + macro dumpTypeImpl(x: typed): untyped = + newLit(x.getTypeImpl.repr) + let t = """ +object + arr: array[0 .. 3, float32] +""" + doAssert(dumpTypeImpl(a) == t) + doAssert(dumpTypeImpl(b) == t) + doAssert(dumpTypeImpl(c) == t) proc getTypeImpl*(n: typedesc): NimNode {.magic: "NGetType", noSideEffect.} - ## Like getType except it includes generic parameters for the implementation + ## Version of ``getTypeImpl`` which takes a ``typedesc``. proc `intVal=`*(n: NimNode, val: BiggestInt) {.magic: "NSetIntVal", noSideEffect.} proc `floatVal=`*(n: NimNode, val: BiggestFloat) {.magic: "NSetFloatVal", noSideEffect.}