updated the manual
This commit is contained in:
parent
3bcafb1c3a
commit
0d234af399
2 changed files with 6 additions and 34 deletions
|
|
@ -208,11 +208,10 @@ Concepts
|
||||||
|
|
||||||
**Note**: Concepts are still in development.
|
**Note**: Concepts are still in development.
|
||||||
|
|
||||||
Concepts, also known as "user-defined type classes", are available in two
|
Concepts, also known as "user-defined type classes", are used to specify an
|
||||||
flavours - declarative and imperative. Both are used to specify an arbitrary
|
arbitrary set of requirements that the matched type must satisfy.
|
||||||
set of requirements that the matched type must satisfy.
|
|
||||||
|
|
||||||
Declarative type classes are written in the following form:
|
Concepts are written in the following form:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
type
|
type
|
||||||
|
|
@ -225,7 +224,7 @@ Declarative type classes are written in the following form:
|
||||||
for value in c:
|
for value in c:
|
||||||
type(value) is T
|
type(value) is T
|
||||||
|
|
||||||
The concept will be matched if:
|
The concept is a match if:
|
||||||
|
|
||||||
a) all of the expressions within the body can be compiled for the tested type
|
a) all of the expressions within the body can be compiled for the tested type
|
||||||
b) all statically evaluatable boolean expressions in the body must be true
|
b) all statically evaluatable boolean expressions in the body must be true
|
||||||
|
|
@ -247,38 +246,14 @@ passed in its explicit ``typedesc[T]`` form:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
type
|
type
|
||||||
OutputStream = concept S
|
OutputStream = concept s
|
||||||
write(var S, string)
|
write(var s, string)
|
||||||
|
|
||||||
Much like generics, concepts are instantiated exactly
|
Much like generics, concepts are instantiated exactly
|
||||||
once for each tested type and any static code included within them is also
|
once for each tested type and any static code included within them is also
|
||||||
executed once.
|
executed once.
|
||||||
|
|
||||||
|
|
||||||
Type inference with type classes
|
|
||||||
--------------------------------
|
|
||||||
|
|
||||||
If a type class is used as the return type of a proc and it won't be bound to
|
|
||||||
a concrete type by some of the proc params, Nim will infer the return type
|
|
||||||
from the proc body. This is usually used with the ``auto`` type class:
|
|
||||||
|
|
||||||
.. code-block:: nim
|
|
||||||
proc makePair(a, b): auto = (first: a, second: b)
|
|
||||||
|
|
||||||
The return type will be treated as an additional generic param and can be
|
|
||||||
explicitly specified at call sites as any other generic param.
|
|
||||||
|
|
||||||
Future versions of Nim may also support overloading based on the return type
|
|
||||||
of the overloads. In such settings, the expected result type at call sites may
|
|
||||||
also influence the inferred return type.
|
|
||||||
|
|
||||||
..
|
|
||||||
Likewise, if a type class is used in another position where Nim expects a
|
|
||||||
concrete type (e.g. a variable declaration or a type coercion), Nim will try
|
|
||||||
to infer the concrete type by applying the matching algorithm that also used
|
|
||||||
in overload resolution.
|
|
||||||
|
|
||||||
|
|
||||||
Symbol lookup in generics
|
Symbol lookup in generics
|
||||||
-------------------------
|
-------------------------
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -66,9 +66,6 @@ When multiple typedesc params are present, they act like a distinct type class
|
||||||
one can use a named alias or an explicit `typedesc` generic param:
|
one can use a named alias or an explicit `typedesc` generic param:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
|
|
||||||
# `type1` and `type2` are aliases for typedesc available from system.nim
|
|
||||||
proc acceptOnlyTypePairs(A, B: type1; C, D: type2)
|
|
||||||
proc acceptOnlyTypePairs[T: typedesc, U: typedesc](A, B: T; C, D: U)
|
proc acceptOnlyTypePairs[T: typedesc, U: typedesc](A, B: T; C, D: U)
|
||||||
|
|
||||||
Once bound, typedesc params can appear in the rest of the proc signature:
|
Once bound, typedesc params can appear in the rest of the proc signature:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue