* 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:
parent
eba8ffcf70
commit
0831292863
4 changed files with 44 additions and 25 deletions
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue