summary refs log tree commit diff stats
path: root/doc
diff options
context:
space:
mode:
authorZahary Karadjov <zahary@gmail.com>2017-05-12 18:43:14 +0300
committerZahary Karadjov <zahary@gmail.com>2017-05-12 18:43:14 +0300
commit2106598d30f990db18c67d87ea08514cf21ffc1c (patch)
tree31c563fe02402fc78cec25e8b773d2f2a39a34f5 /doc
parentf4e73344d4b6bcc7282d363a008307d5482f2a22 (diff)
downloadNim-2106598d30f990db18c67d87ea08514cf21ffc1c.tar.gz
explain covariance in the manual
Diffstat (limited to 'doc')
-rw-r--r--doc/manual/type_rel.txt16
1 files changed, 16 insertions, 0 deletions
diff --git a/doc/manual/type_rel.txt b/doc/manual/type_rel.txt
index 5b68f73aa..1a753d773 100644
--- a/doc/manual/type_rel.txt
+++ b/doc/manual/type_rel.txt
@@ -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}