Skip to content

Commit c05d1aa

Browse files
authored
Add more runnableExamples (#16864)
Add more links
1 parent 13640c0 commit c05d1aa

File tree

1 file changed

+80
-30
lines changed

1 file changed

+80
-30
lines changed

lib/pure/typetraits.nim

Lines changed: 80 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,106 +15,152 @@
1515
import std/private/since
1616
export system.`$` # for backward compatibility
1717

18-
proc name*(t: typedesc): string {.magic: "TypeTrait".}
18+
proc name*(t: typedesc): string {.magic: "TypeTrait".} =
1919
## Returns the name of the given type.
2020
##
21-
## Alias for system.`$`(t) since Nim v0.20.
21+
## Alias for `system.\`$\`(t) <dollars.html#$,typedesc>`_ since Nim v0.20.
22+
runnableExamples:
23+
doAssert name(int) == "int"
24+
doAssert name(seq[string]) == "seq[string]"
2225

2326
proc arity*(t: typedesc): int {.magic: "TypeTrait".} =
2427
## Returns the arity of the given type. This is the number of "type"
25-
## components or the number of generic parameters a given type ``t`` has.
28+
## components or the number of generic parameters a given type `t` has.
2629
runnableExamples:
27-
assert arity(seq[string]) == 1
28-
assert arity(array[3, int]) == 2
29-
assert arity((int, int, float, string)) == 4
30+
doAssert arity(int) == 0
31+
doAssert arity(seq[string]) == 1
32+
doAssert arity(array[3, int]) == 2
33+
doAssert arity((int, int, float, string)) == 4
3034

3135
proc genericHead*(t: typedesc): typedesc {.magic: "TypeTrait".} =
3236
## Accepts an instantiated generic type and returns its
3337
## uninstantiated form.
3438
## A compile-time error will be produced if the supplied type
3539
## is not generic.
3640
##
37-
## See also:
38-
## * `stripGenericParams <#stripGenericParams,typedesc>`_
41+
## **See also:**
42+
## * `stripGenericParams proc <#stripGenericParams,typedesc>`_
3943
runnableExamples:
4044
type
4145
Foo[T] = object
4246
FooInst = Foo[int]
4347
Foo2 = genericHead(FooInst)
48+
4449
doAssert Foo2 is Foo and Foo is Foo2
4550
doAssert genericHead(Foo[seq[string]]) is Foo
4651
doAssert not compiles(genericHead(int))
4752

4853
type Generic = concept f
4954
type _ = genericHead(typeof(f))
55+
5056
proc bar(a: Generic): typeof(a) = a
57+
5158
doAssert bar(Foo[string].default) == Foo[string]()
5259
doAssert not compiles bar(string.default)
5360

5461
when false: # these don't work yet
5562
doAssert genericHead(Foo[int])[float] is Foo[float]
5663
doAssert seq[int].genericHead is seq
5764

58-
proc stripGenericParams*(t: typedesc): typedesc {.magic: "TypeTrait".}
65+
proc stripGenericParams*(t: typedesc): typedesc {.magic: "TypeTrait".} =
5966
## This trait is similar to `genericHead <#genericHead,typedesc>`_, but
60-
## instead of producing error for non-generic types, it will just return
67+
## instead of producing an error for non-generic types, it will just return
6168
## them unmodified.
69+
runnableExamples:
70+
type Foo[T] = object
71+
72+
doAssert stripGenericParams(Foo[string]) is Foo
73+
doAssert stripGenericParams(int) is int
6274

6375
proc supportsCopyMem*(t: typedesc): bool {.magic: "TypeTrait".}
64-
## This trait returns true if the type ``t`` is safe to use for
76+
## This trait returns true if the type `t` is safe to use for
6577
## `copyMem`:idx:.
6678
##
6779
## Other languages name a type like these `blob`:idx:.
6880

69-
proc isNamedTuple*(T: typedesc): bool {.magic: "TypeTrait".}
70-
## Return true for named tuples, false for any other type.
81+
proc isNamedTuple*(T: typedesc): bool {.magic: "TypeTrait".} =
82+
## Returns true for named tuples, false for any other type.
83+
runnableExamples:
84+
doAssert not isNamedTuple(int)
85+
doAssert not isNamedTuple((string, int))
86+
doAssert isNamedTuple(tuple[name: string, age: int])
87+
88+
proc distinctBase*(T: typedesc): typedesc {.magic: "TypeTrait".} =
89+
## Returns the base type for distinct types. This works only
90+
## for distinct types and produces a compile time error otherwise.
91+
##
92+
## **See also:**
93+
## * `distinctBase template <#distinctBase.t,T>`_
94+
runnableExamples:
95+
type MyInt = distinct int
7196

72-
proc distinctBase*(T: typedesc): typedesc {.magic: "TypeTrait".}
73-
## Returns base type for distinct types, works only for distinct types.
74-
## compile time error otherwise
97+
doAssert distinctBase(MyInt) is int
98+
doAssert not compiles(distinctBase(int))
7599

76100
since (1, 1):
77101
template distinctBase*[T](a: T): untyped =
78-
## overload for values
102+
## Overload of `distinctBase <#distinctBase,typedesc>`_ for values.
79103
runnableExamples:
80104
type MyInt = distinct int
105+
81106
doAssert 12.MyInt.distinctBase == 12
107+
82108
distinctBase(type(a))(a)
83109

84-
proc tupleLen*(T: typedesc[tuple]): int {.magic: "TypeTrait".}
85-
## Return number of elements of `T`
110+
proc tupleLen*(T: typedesc[tuple]): int {.magic: "TypeTrait".} =
111+
## Returns the number of elements of the tuple type `T`.
112+
##
113+
## **See also:**
114+
## * `tupleLen template <#tupleLen.t>`_
115+
runnableExamples:
116+
doAssert tupleLen((int, int, float, string)) == 4
117+
doAssert tupleLen(tuple[name: string, age: int]) == 2
86118

87119
template tupleLen*(t: tuple): int =
88-
## Return number of elements of `t`
120+
## Returns the number of elements of the tuple `t`.
121+
##
122+
## **See also:**
123+
## * `tupleLen proc <#tupleLen,typedesc>`_
124+
runnableExamples:
125+
doAssert tupleLen((1, 2)) == 2
126+
89127
tupleLen(type(t))
90128

91129
template get*(T: typedesc[tuple], i: static int): untyped =
92-
## Return `i`\th element of `T`
130+
## Returns the `i`-th element of `T`.
93131
# Note: `[]` currently gives: `Error: no generic parameters allowed for ...`
132+
runnableExamples:
133+
doAssert get((int, int, float, string), 2) is float
134+
94135
type(default(T)[i])
95136

96137
type StaticParam*[value: static type] = object
97-
## used to wrap a static value in `genericParams`
138+
## Used to wrap a static value in `genericParams <#genericParams.t,typedesc>`_.
98139

99140
since (1, 3, 5):
100141
template elementType*(a: untyped): typedesc =
101-
## return element type of `a`, which can be any iterable (over which you
102-
## can iterate)
142+
## Returns the element type of `a`, which can be any iterable (over which you
143+
## can iterate).
103144
runnableExamples:
104145
iterator myiter(n: int): auto =
105-
for i in 0..<n: yield i
146+
for i in 0 ..< n:
147+
yield i
148+
106149
doAssert elementType(@[1,2]) is int
107150
doAssert elementType("asdf") is char
108151
doAssert elementType(myiter(3)) is int
152+
109153
typeof(block: (for ai in a: ai))
110154

111155
import std/macros
112156

113157
macro enumLen*(T: typedesc[enum]): int =
114158
## Returns the number of items in the enum `T`.
115-
116159
runnableExamples:
117-
type Foo = enum fooItem1 fooItem2
160+
type Foo = enum
161+
fooItem1
162+
fooItem2
163+
118164
doAssert Foo.enumLen == 2
119165

120166
let bracketExpr = getType(T)
@@ -179,19 +225,23 @@ macro genericParamsImpl(T: typedesc): untyped =
179225

180226
since (1, 1):
181227
template genericParams*(T: typedesc): untyped =
182-
## return tuple of generic params for generic `T`
228+
## Returns the tuple of generic parameters for the generic type `T`.
229+
##
230+
## **Note:** For the builtin array type, the index generic parameter will
231+
## **always** become a range type after it's bound to a variable.
183232
runnableExamples:
184233
type Foo[T1, T2] = object
234+
185235
doAssert genericParams(Foo[float, string]) is (float, string)
236+
186237
type Bar[N: static float, T] = object
238+
187239
doAssert genericParams(Bar[1.0, string]) is (StaticParam[1.0], string)
188240
doAssert genericParams(Bar[1.0, string]).get(0).value == 1.0
189241
doAssert genericParams(seq[Bar[2.0, string]]).get(0) is Bar[2.0, string]
190242
var s: seq[Bar[3.0, string]]
191243
doAssert genericParams(typeof(s)) is (Bar[3.0, string],)
192244

193-
# NOTE: For the builtin array type, the index generic param will
194-
# **always** become a range type after it's bound to a variable.
195245
doAssert genericParams(array[10, int]) is (StaticParam[10], int)
196246
var a: array[10, int]
197247
doAssert genericParams(typeof(a)) is (range[0..9], int)

0 commit comments

Comments
 (0)