clarify the meaning of the 'auto' metatype; 'auto' is now bind-multiple; fixes #3224
This commit is contained in:
parent
f79ec6cdf5
commit
0aa908c86c
7 changed files with 91 additions and 55 deletions
|
|
@ -116,7 +116,7 @@ type class matches
|
|||
``array`` any array type
|
||||
``set`` any set type
|
||||
``seq`` any seq type
|
||||
``auto`` any type
|
||||
``any`` any type
|
||||
================== ===================================================
|
||||
|
||||
Furthermore, every generic type automatically creates a type class of the same
|
||||
|
|
@ -163,15 +163,6 @@ module to illustrate this:
|
|||
Alternatively, the ``distinct`` type modifier can be applied to the type class
|
||||
to allow each param matching the type class to bind to a different type.
|
||||
|
||||
If a proc param doesn't have a type specified, Nim will use the
|
||||
``distinct auto`` type class (also known as ``any``). Note this behavior is
|
||||
deprecated for procs; templates, however, support them:
|
||||
|
||||
.. code-block:: nim
|
||||
# allow any combination of param types
|
||||
proc concat(a, b): string = $a & $b # deprecated
|
||||
proc concat(a, b: any): string = $a & $b # preferred
|
||||
|
||||
Procs written with the implicitly generic style will often need to refer to the
|
||||
type parameters of the matched generic type. They can be easily accessed using
|
||||
the dot syntax:
|
||||
|
|
|
|||
|
|
@ -1228,3 +1228,27 @@ However, a ``void`` type cannot be inferred in generic code:
|
|||
|
||||
The ``void`` type is only valid for parameters and return types; other symbols
|
||||
cannot have the type ``void``.
|
||||
|
||||
|
||||
Auto type
|
||||
---------
|
||||
|
||||
The ``auto`` type can only be used for return types and parameters. For return
|
||||
types it causes the compiler to infer the type from the routine body:
|
||||
|
||||
.. code-block:: nim
|
||||
proc returnsInt(): auto = 1984
|
||||
|
||||
For parameters it currently creates implicitly generic routines:
|
||||
|
||||
.. code-block:: nim
|
||||
proc foo(a, b: auto) = discard
|
||||
|
||||
Is the same as:
|
||||
|
||||
.. code-block:: nim
|
||||
proc foo[T1, T2](a: T1, b: T2) = discard
|
||||
|
||||
However later versions of the language might change this to mean "infer the
|
||||
parameters' types from the body". Then the above ``foo`` would be rejected as
|
||||
the parameters' types can not be infered from an empty ``discard`` statement.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue