summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorTimothee Cour <timothee.cour2@gmail.com>2018-12-29 16:09:47 -0800
committerAndreas Rumpf <rumpf_a@web.de>2018-12-30 01:09:47 +0100
commit083129286349ba440018cff1ed20172b675b84fe (patch)
tree812b2c252fd8947f6e5546991e5c6220703bfc9d
parenteba8ffcf70cca5dd802c5d33f6ecea814829f9fc (diff)
downloadNim-083129286349ba440018cff1ed20172b675b84fe.tar.gz
revives: Move typetraits.`$` to system. Fixes #5827 (#10071)
* Move typetraits.`$` to system. Fixes #5827.
* revive PR; adjust code to make sure everything works and add tests
* fix tests/concepts/tstackconcept.nim
* address comments
-rw-r--r--compiler/semmagic.nim2
-rw-r--r--lib/pure/typetraits.nim44
-rw-r--r--lib/system.nim9
-rw-r--r--tests/system/tsystem_misc.nim14
4 files changed, 44 insertions, 25 deletions
diff --git a/compiler/semmagic.nim b/compiler/semmagic.nim
index 7e61854b8..568b418e9 100644
--- a/compiler/semmagic.nim
+++ b/compiler/semmagic.nim
@@ -136,7 +136,7 @@ proc evalTypeTrait(c: PContext; traitCall: PNode, operand: PType, context: PSym)
     return typeWithSonsResult(tyAnd, @[operand, operand2])
   of "not":
     return typeWithSonsResult(tyNot, @[operand])
-  of "name":
+  of "name", "$":
     result = newStrNode(nkStrLit, operand.typeToString(preferTypeName))
     result.typ = newType(tyString, context)
     result.info = traitCall.info
diff --git a/lib/pure/typetraits.nim b/lib/pure/typetraits.nim
index c53b68864..db57ac02d 100644
--- a/lib/pure/typetraits.nim
+++ b/lib/pure/typetraits.nim
@@ -11,29 +11,7 @@
 ## working with types
 
 proc name*(t: typedesc): string {.magic: "TypeTrait".}
-  ## Returns the name of the given type.
-  ##
-  ## Example:
-  ##
-  ## .. code-block::
-  ##
-  ##   import typetraits
-  ##
-  ##   proc `$`*(T: typedesc): string = name(T)
-  ##
-  ##   template test(x): typed =
-  ##     echo "type: ", type(x), ", value: ", x
-  ##
-  ##   test 42
-  ##   # --> type: int, value: 42
-  ##   test "Foo"
-  ##   # --> type: string, value: Foo
-  ##   test(@['A','B'])
-  ##   # --> type: seq[char], value: @[A, B]
-
-proc `$`*(t: typedesc): string =
-  ## An alias for `name`.
-  name(t)
+  ## Alias for system.`$`(t) since Nim v0.20.0.
 
 proc arity*(t: typedesc): int {.magic: "TypeTrait".} =
   ## Returns the arity of the given type. This is the number of "type" components or
@@ -64,4 +42,22 @@ proc supportsCopyMem*(t: typedesc): bool {.magic: "TypeTrait".}
 
 
 when isMainModule:
-  doAssert $type(42) == "int"
+  static:
+    doAssert $type(42) == "int"
+    doAssert int.name == "int"
+
+  const a1 = name(int)
+  const a2 = $(int)
+  const a3 = $int
+  doAssert a1 == "int"
+  doAssert a2 == "int"
+  doAssert a3 == "int"
+
+  proc fun[T: typedesc](t: T) =
+    const a1 = name(t)
+    const a2 = $(t)
+    const a3 = $t
+    doAssert a1 == "int"
+    doAssert a2 == "int"
+    doAssert a3 == "int"
+  fun(int)
diff --git a/lib/system.nim b/lib/system.nim
index 761794da6..bcd5fe05a 100644
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -4431,3 +4431,12 @@ when defined(genode):
       componentConstructHook(env)
         # Perform application initialization
         # and return to thread entrypoint.
+
+proc `$`*(t: typedesc): string {.magic: "TypeTrait".} =
+  ## Returns the name of the given type.
+  ##
+  ## For more procedures dealing with ``typedesc``, see ``typetraits.nim``.
+  runnableExamples:
+    doAssert $(type(42)) == "int"
+    doAssert $(type("Foo")) == "string"
+    static: doAssert $(type(@['A', 'B'])) == "seq[char]"
diff --git a/tests/system/tsystem_misc.nim b/tests/system/tsystem_misc.nim
index 3bbb5eff1..98bc3f4a3 100644
--- a/tests/system/tsystem_misc.nim
+++ b/tests/system/tsystem_misc.nim
@@ -30,6 +30,20 @@ discard """
 '''
 """
 
+
+block:
+  const a2 = $(int)
+  const a3 = $int
+  doAssert a2 == "int"
+  doAssert a3 == "int"
+
+  proc fun[T: typedesc](t: T) =
+    const a2 = $(t)
+    const a3 = $t
+    doAssert a2 == "int"
+    doAssert a3 == "int"
+  fun(int)
+
 # check high/low implementations
 doAssert high(int) > low(int)
 doAssert high(int8) > low(int8)