diff --git a/changelog.md b/changelog.md index 93fbd6e0c2..c113505cdf 100644 --- a/changelog.md +++ b/changelog.md @@ -35,3 +35,4 @@ - ``rationals.toRational`` now uses an algorithm based on continued fractions. This means its results are more precise and it can't run into an infinite loop anymore. +- Added ``typetraits.$`` as an alias for ``typetraits.name``. diff --git a/lib/pure/typetraits.nim b/lib/pure/typetraits.nim index 8d738f9a75..3b6f7de1ac 100644 --- a/lib/pure/typetraits.nim +++ b/lib/pure/typetraits.nim @@ -31,6 +31,10 @@ proc name*(t: typedesc): string {.magic: "TypeTrait".} ## test(@['A','B']) ## # --> type: seq[char], value: @[A, B] +proc `$`*(t: typedesc): string = + ## An alias for `name`. + name(t) + proc arity*(t: typedesc): int {.magic: "TypeTrait".} ## Returns the arity of the given type @@ -52,3 +56,12 @@ proc stripGenericParams*(t: typedesc): typedesc {.magic: "TypeTrait".} proc supportsCopyMem*(t: typedesc): bool {.magic: "TypeTrait".} ## This trait returns true iff the type ``t`` is safe to use for ## `copyMem`:idx:. Other languages name a type like these `blob`:idx:. + + +when isMainModule: + # echo type(42) + import streams + var ss = newStringStream() + ss.write($type(42)) # needs `$` + ss.setPosition(0) + doAssert ss.readAll() == "int"