summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorZahary Karadjov <zahary@gmail.com>2016-08-25 02:14:25 +0300
committerZahary Karadjov <zahary@gmail.com>2017-03-24 16:59:47 +0200
commit52b241fd5703034066d9c9f3c3d514162c2c809e (patch)
tree41debbc16eabfed6784ee557678c3f7c8b4e397f /lib
parentfe48dd1cbec500298f7edeb75f1d6fef8490346c (diff)
downloadNim-52b241fd5703034066d9c9f3c3d514162c2c809e.tar.gz
new type traits: `GenericHead` and `StripGenericParams`
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/typetraits.nim19
1 files changed, 17 insertions, 2 deletions
diff --git a/lib/pure/typetraits.nim b/lib/pure/typetraits.nim
index 2c3d872df..cc7fb9f72 100644
--- a/lib/pure/typetraits.nim
+++ b/lib/pure/typetraits.nim
@@ -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
+