explain covariance in the manual

This commit is contained in:
Zahary Karadjov
2017-05-12 18:43:14 +03:00
parent f4e73344d4
commit 2106598d30

View File

@@ -111,6 +111,20 @@ relation is extended to the types ``var``, ``ref``, ``ptr``:
.. 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
--------------------
A type ``a`` is **implicitly** convertible to type ``b`` iff the following
@@ -119,6 +133,8 @@ algorithm returns true:
.. code-block:: nim
# XXX range types?
proc isImplicitlyConvertible(a, b: PType): bool =
if isSubtype(a, b) or isCovariant(a, b):
return true
case a.kind
of int: result = b in {int8, int16, int32, int64, uint, uint8, uint16,
uint32, uint64, float, float32, float64}