Skip to content

Commit 0831292

Browse files
timotheecourAraq
authored andcommitted
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
1 parent eba8ffc commit 0831292

File tree

4 files changed

+44
-25
lines changed

4 files changed

+44
-25
lines changed

compiler/semmagic.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ proc evalTypeTrait(c: PContext; traitCall: PNode, operand: PType, context: PSym)
136136
return typeWithSonsResult(tyAnd, @[operand, operand2])
137137
of "not":
138138
return typeWithSonsResult(tyNot, @[operand])
139-
of "name":
139+
of "name", "$":
140140
result = newStrNode(nkStrLit, operand.typeToString(preferTypeName))
141141
result.typ = newType(tyString, context)
142142
result.info = traitCall.info

lib/pure/typetraits.nim

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,7 @@
1111
## working with types
1212

1313
proc name*(t: typedesc): string {.magic: "TypeTrait".}
14-
## Returns the name of the given type.
15-
##
16-
## Example:
17-
##
18-
## .. code-block::
19-
##
20-
## import typetraits
21-
##
22-
## proc `$`*(T: typedesc): string = name(T)
23-
##
24-
## template test(x): typed =
25-
## echo "type: ", type(x), ", value: ", x
26-
##
27-
## test 42
28-
## # --> type: int, value: 42
29-
## test "Foo"
30-
## # --> type: string, value: Foo
31-
## test(@['A','B'])
32-
## # --> type: seq[char], value: @[A, B]
33-
34-
proc `$`*(t: typedesc): string =
35-
## An alias for `name`.
36-
name(t)
14+
## Alias for system.`$`(t) since Nim v0.20.0.
3715

3816
proc arity*(t: typedesc): int {.magic: "TypeTrait".} =
3917
## 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".}
6442

6543

6644
when isMainModule:
67-
doAssert $type(42) == "int"
45+
static:
46+
doAssert $type(42) == "int"
47+
doAssert int.name == "int"
48+
49+
const a1 = name(int)
50+
const a2 = $(int)
51+
const a3 = $int
52+
doAssert a1 == "int"
53+
doAssert a2 == "int"
54+
doAssert a3 == "int"
55+
56+
proc fun[T: typedesc](t: T) =
57+
const a1 = name(t)
58+
const a2 = $(t)
59+
const a3 = $t
60+
doAssert a1 == "int"
61+
doAssert a2 == "int"
62+
doAssert a3 == "int"
63+
fun(int)

lib/system.nim

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4431,3 +4431,12 @@ when defined(genode):
44314431
componentConstructHook(env)
44324432
# Perform application initialization
44334433
# and return to thread entrypoint.
4434+
4435+
proc `$`*(t: typedesc): string {.magic: "TypeTrait".} =
4436+
## Returns the name of the given type.
4437+
##
4438+
## For more procedures dealing with ``typedesc``, see ``typetraits.nim``.
4439+
runnableExamples:
4440+
doAssert $(type(42)) == "int"
4441+
doAssert $(type("Foo")) == "string"
4442+
static: doAssert $(type(@['A', 'B'])) == "seq[char]"

tests/system/tsystem_misc.nim

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ discard """
3030
'''
3131
"""
3232

33+
34+
block:
35+
const a2 = $(int)
36+
const a3 = $int
37+
doAssert a2 == "int"
38+
doAssert a3 == "int"
39+
40+
proc fun[T: typedesc](t: T) =
41+
const a2 = $(t)
42+
const a3 = $t
43+
doAssert a2 == "int"
44+
doAssert a3 == "int"
45+
fun(int)
46+
3347
# check high/low implementations
3448
doAssert high(int) > low(int)
3549
doAssert high(int8) > low(int8)

0 commit comments

Comments
 (0)