mirror of
https://github.com/nim-lang/Nim.git
synced 2026-02-14 15:23:27 +00:00
document 'var T' and 'typedesc' restriction in generics; closes #1156
This commit is contained in:
@@ -4387,6 +4387,34 @@ be inferred to have the equivalent of the `any` type class and thus they will
|
||||
match anything without discrimination.
|
||||
|
||||
|
||||
Generic inference restrictions
|
||||
------------------------------
|
||||
|
||||
The types ``var T`` and ``typedesc[T]`` cannot be inferred in a generic
|
||||
instantiation. The following is not allowed:
|
||||
|
||||
.. code-block:: nim
|
||||
:test: "nim c $1"
|
||||
:status: 1
|
||||
|
||||
proc g[T](f: proc(x: T); x: T) =
|
||||
f(x)
|
||||
|
||||
proc c(y: int) = echo y
|
||||
proc v(y: var int) =
|
||||
y += 100
|
||||
var i: int
|
||||
|
||||
# allowed: infers 'T' to be of type 'int'
|
||||
g(c, 42)
|
||||
|
||||
# not valid: 'T' is not inferred to be of type 'var int'
|
||||
g(v, i)
|
||||
|
||||
# also not allowed: explict instantiation via 'var int'
|
||||
g[var int](v, i)
|
||||
|
||||
|
||||
Concepts
|
||||
--------
|
||||
|
||||
|
||||
Reference in New Issue
Block a user