typetraits: make genericHead docs reflect reality; use runnableExamples (#16776) [backport:1.4]
This commit is contained in:
parent
2d0cb18b9f
commit
8f1f0bd1da
2 changed files with 24 additions and 17 deletions
|
|
@ -28,29 +28,32 @@ proc arity*(t: typedesc): int {.magic: "TypeTrait".} =
|
||||||
assert arity(array[3, int]) == 2
|
assert arity(array[3, int]) == 2
|
||||||
assert arity((int, int, float, string)) == 4
|
assert arity((int, int, float, string)) == 4
|
||||||
|
|
||||||
proc genericHead*(t: typedesc): typedesc {.magic: "TypeTrait".}
|
proc genericHead*(t: typedesc): typedesc {.magic: "TypeTrait".} =
|
||||||
## Accepts an instantiated generic type and returns its
|
## Accepts an instantiated generic type and returns its
|
||||||
## uninstantiated form.
|
## 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
|
## A compile-time error will be produced if the supplied type
|
||||||
## is not generic.
|
## is not generic.
|
||||||
##
|
##
|
||||||
## See also:
|
## See also:
|
||||||
## * `stripGenericParams <#stripGenericParams,typedesc>`_
|
## * `stripGenericParams <#stripGenericParams,typedesc>`_
|
||||||
##
|
runnableExamples:
|
||||||
## Example:
|
type
|
||||||
##
|
Foo[T] = object
|
||||||
## .. code-block:: nim
|
FooInst = Foo[int]
|
||||||
## type
|
Foo2 = genericHead(FooInst)
|
||||||
## Functor[A] = concept f
|
doAssert Foo2 is Foo and Foo is Foo2
|
||||||
## type MatchedGenericType = genericHead(typeof(f))
|
doAssert genericHead(Foo[seq[string]]) is Foo
|
||||||
## # `f` will be a value of a type such as `Option[T]`
|
doAssert not compiles(genericHead(int))
|
||||||
## # `MatchedGenericType` will become the `Option` type
|
|
||||||
|
|
||||||
|
type Generic = concept f
|
||||||
|
type _ = genericHead(typeof(f))
|
||||||
|
proc bar(a: Generic): typeof(a) = a
|
||||||
|
doAssert bar(Foo[string].default) == Foo[string]()
|
||||||
|
doAssert not compiles bar(string.default)
|
||||||
|
|
||||||
|
when false: # these don't work yet
|
||||||
|
doAssert genericHead(Foo[int])[float] is Foo[float]
|
||||||
|
doAssert seq[int].genericHead is seq
|
||||||
|
|
||||||
proc stripGenericParams*(t: typedesc): typedesc {.magic: "TypeTrait".}
|
proc stripGenericParams*(t: typedesc): typedesc {.magic: "TypeTrait".}
|
||||||
## This trait is similar to `genericHead <#genericHead,typedesc>`_, but
|
## This trait is similar to `genericHead <#genericHead,typedesc>`_, but
|
||||||
|
|
@ -168,7 +171,6 @@ macro genericParamsImpl(T: typedesc): untyped =
|
||||||
ret = newTree(nnkBracketExpr, @[bindSym"range", ai])
|
ret = newTree(nnkBracketExpr, @[bindSym"range", ai])
|
||||||
else:
|
else:
|
||||||
since (1, 1):
|
since (1, 1):
|
||||||
echo ai.typeKind
|
|
||||||
ret = newTree(nnkBracketExpr, @[bindSym"StaticParam", ai])
|
ret = newTree(nnkBracketExpr, @[bindSym"StaticParam", ai])
|
||||||
result.add ret
|
result.add ret
|
||||||
break
|
break
|
||||||
|
|
|
||||||
|
|
@ -282,7 +282,12 @@ block genericHead:
|
||||||
doAssert not compiles(genericHead(Foo))
|
doAssert not compiles(genericHead(Foo))
|
||||||
type Bar = object
|
type Bar = object
|
||||||
doAssert not compiles(genericHead(Bar))
|
doAssert not compiles(genericHead(Bar))
|
||||||
# doAssert seq[int].genericHead is seq
|
|
||||||
|
when false: # xxx not supported yet
|
||||||
|
doAssert seq[int].genericHead is seq
|
||||||
|
when false: # xxx not supported yet, gives: Error: identifier expected
|
||||||
|
type Hoo[T] = object
|
||||||
|
doAssert genericHead(Hoo[int])[float] is Hoo[float]
|
||||||
|
|
||||||
block: # elementType
|
block: # elementType
|
||||||
iterator myiter(n: int): auto =
|
iterator myiter(n: int): auto =
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue