@@ -28,29 +28,32 @@ proc arity*(t: typedesc): int {.magic: "TypeTrait".} =
2828 assert arity (array [3 , int ]) == 2
2929 assert arity ((int , int , float , string )) == 4
3030
31- proc genericHead * (t: typedesc ): typedesc {.magic : " TypeTrait" .}
31+ proc genericHead * (t: typedesc ): typedesc {.magic : " TypeTrait" .} =
3232 # # Accepts an instantiated generic type and returns its
3333 # # uninstantiated form.
34- # #
35- # # For example:
36- # # * `seq[int].genericHead` will be just `seq`
37- # # * `seq[int].genericHead[float]` will be `seq[float]`
38- # #
3934 # # A compile-time error will be produced if the supplied type
4035 # # is not generic.
4136 # #
4237 # # See also:
4338 # # * `stripGenericParams <#stripGenericParams,typedesc>`_
44- # #
45- # # Example:
46- # #
47- # # .. code-block:: nim
48- # # type
49- # # Functor[A] = concept f
50- # # type MatchedGenericType = genericHead(typeof(f))
51- # # # `f` will be a value of a type such as `Option[T]`
52- # # # `MatchedGenericType` will become the `Option` type
53-
39+ runnableExamples:
40+ type
41+ Foo [T] = object
42+ FooInst = Foo [int ]
43+ Foo2 = genericHead (FooInst )
44+ doAssert Foo2 is Foo and Foo is Foo2
45+ doAssert genericHead (Foo [seq [string ]]) is Foo
46+ doAssert not compiles (genericHead (int ))
47+
48+ type Generic = concept f
49+ type _ = genericHead (typeof (f))
50+ proc bar (a: Generic ): typeof (a) = a
51+ doAssert bar (Foo [string ].default) == Foo [string ]()
52+ doAssert not compiles bar (string .default)
53+
54+ when false : # these don't work yet
55+ doAssert genericHead (Foo [int ])[float ] is Foo [float ]
56+ doAssert seq [int ].genericHead is seq
5457
5558proc stripGenericParams * (t: typedesc ): typedesc {.magic : " TypeTrait" .}
5659 # # This trait is similar to `genericHead <#genericHead,typedesc>`_, but
@@ -168,7 +171,6 @@ macro genericParamsImpl(T: typedesc): untyped =
168171 ret = newTree (nnkBracketExpr, @ [bindSym " range" , ai])
169172 else :
170173 since (1 , 1 ):
171- echo ai.typeKind
172174 ret = newTree (nnkBracketExpr, @ [bindSym " StaticParam" , ai])
173175 result .add ret
174176 break
0 commit comments