diff options
author | Timothee Cour <timothee.cour2@gmail.com> | 2018-12-29 16:09:47 -0800 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2018-12-30 01:09:47 +0100 |
commit | 083129286349ba440018cff1ed20172b675b84fe (patch) | |
tree | 812b2c252fd8947f6e5546991e5c6220703bfc9d /lib/pure | |
parent | eba8ffcf70cca5dd802c5d33f6ecea814829f9fc (diff) | |
download | Nim-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
Diffstat (limited to 'lib/pure')
-rw-r--r-- | lib/pure/typetraits.nim | 44 |
1 files changed, 20 insertions, 24 deletions
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) |