explain covariance in the manual
This commit is contained in:
parent
f4e73344d4
commit
2106598d30
1 changed files with 16 additions and 0 deletions
|
|
@ -111,6 +111,20 @@ relation is extended to the types ``var``, ``ref``, ``ptr``:
|
||||||
.. XXX nil is a special value!
|
.. XXX nil is a special value!
|
||||||
|
|
||||||
|
|
||||||
|
Covariance
|
||||||
|
----------
|
||||||
|
|
||||||
|
Covariance in Nim can be introduced only though pointer-like types such
|
||||||
|
as ``ptr`` and ``ref``. Sequence, Array and OpenArray types, instantiated
|
||||||
|
with pointer-like types will be considered covariant if and only if they
|
||||||
|
are also immutable. The introduction of a ``var`` modifier or addional
|
||||||
|
``ptr`` or ``ref`` indirections would result in invariant treatment of
|
||||||
|
these types.
|
||||||
|
|
||||||
|
``proc`` types are curently always invariant, but future version of Nim
|
||||||
|
may relax this rule.
|
||||||
|
|
||||||
|
|
||||||
Convertible relation
|
Convertible relation
|
||||||
--------------------
|
--------------------
|
||||||
A type ``a`` is **implicitly** convertible to type ``b`` iff the following
|
A type ``a`` is **implicitly** convertible to type ``b`` iff the following
|
||||||
|
|
@ -119,6 +133,8 @@ algorithm returns true:
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
# XXX range types?
|
# XXX range types?
|
||||||
proc isImplicitlyConvertible(a, b: PType): bool =
|
proc isImplicitlyConvertible(a, b: PType): bool =
|
||||||
|
if isSubtype(a, b) or isCovariant(a, b):
|
||||||
|
return true
|
||||||
case a.kind
|
case a.kind
|
||||||
of int: result = b in {int8, int16, int32, int64, uint, uint8, uint16,
|
of int: result = b in {int8, int16, int32, int64, uint, uint8, uint16,
|
||||||
uint32, uint64, float, float32, float64}
|
uint32, uint64, float, float32, float64}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue