mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-18 05:20:31 +00:00
explain covariance in the manual
This commit is contained in:
@@ -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}
|
||||
|
||||
Reference in New Issue
Block a user