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
This commit is contained in:
Timothee Cour 2018-12-29 16:09:47 -08:00 • committed by Andreas Rumpf
commit 0831292863
4 changed files with 44 additions and 25 deletions

View file

@ -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)