new type traits: GenericHead and StripGenericParams

This commit is contained in:
Zahary Karadjov 2016-08-25 02:14:25 +03:00
commit 52b241fd57
3 changed files with 62 additions and 5 deletions

View file

@ -19,7 +19,7 @@ proc name*(t: typedesc): string {.magic: "TypeTrait".}
##
## import typetraits
##
## proc `$`*[T](some:typedesc[T]): string = name(T)
## proc `$`*(T: typedesc): string = name(T)
##
## template test(x): stmt =
## echo "type: ", type(x), ", value: ", x
@ -31,6 +31,21 @@ proc name*(t: typedesc): string {.magic: "TypeTrait".}
## test(@['A','B'])
## # --> type: seq[char], value: @[A, B]
proc arity*(t: typedesc): int {.magic: "TypeTrait".}
## Returns the arity of the given type
proc GenericHead*(t: typedesc): typedesc {.magic: "TypeTrait".}
## Accepts an instantiated generic type and returns its
## uninstantiated form.
##
## For example:
## seq[int].GenericHead will be just seq
## seq[int].GenericHead[float] will be seq[float]
##
## A compile-time error will be produced if the supplied type
## is not generic
proc StripGenericParams*(t: typedesc): typedesc {.magic: "TypeTrait".}
## This trait is similar to `GenericHead`, but instead of producing
## error for non-generic types, it will just return them unmodified