manual: do not mention the VTable types which are not implemented yet

This commit is contained in:
Andreas Rumpf 2018-02-02 08:07:31 +01:00
commit 7fc80f8f86

View file

@ -582,60 +582,61 @@ the concept body:
proc log(format: static[string], varargs[distinct StringRef]) proc log(format: static[string], varargs[distinct StringRef])
VTable types ..
------------ VTable types
------------
Concepts allow Nim to define a great number of algorithms, using only Concepts allow Nim to define a great number of algorithms, using only
static polymorphism and without erasing any type information or sacrificing static polymorphism and without erasing any type information or sacrificing
any execution speed. But when polymorphic collections of objects are required, any execution speed. But when polymorphic collections of objects are required,
the user must use one of the provided type erasure techniques - either common the user must use one of the provided type erasure techniques - either common
base types or VTable types. base types or VTable types.
VTable types are represented as "fat pointers" storing a reference to an VTable types are represented as "fat pointers" storing a reference to an
object together with a reference to a table of procs implementing a set of object together with a reference to a table of procs implementing a set of
required operations (the so called vtable). required operations (the so called vtable).
In contrast to other programming languages, the vtable in Nim is stored In contrast to other programming languages, the vtable in Nim is stored
externally to the object, allowing you to create multiple different vtable externally to the object, allowing you to create multiple different vtable
views for the same object. Thus, the polymorphism in Nim is unbounded - views for the same object. Thus, the polymorphism in Nim is unbounded -
any type can implement an unlimited number of protocols or interfaces not any type can implement an unlimited number of protocols or interfaces not
originally envisioned by the type's author. originally envisioned by the type's author.
Any concept type can be turned into a VTable type by using the ``vtref`` Any concept type can be turned into a VTable type by using the ``vtref``
or the ``vtptr`` compiler magics. Under the hood, these magics generate or the ``vtptr`` compiler magics. Under the hood, these magics generate
a converter type class, which converts the regular instances of the matching a converter type class, which converts the regular instances of the matching
types to the corresponding VTable type. types to the corresponding VTable type.
.. code-block:: nim .. code-block:: nim
type type
IntEnumerable = vtref Enumerable[int] IntEnumerable = vtref Enumerable[int]
MyObject = object MyObject = object
enumerables: seq[IntEnumerable] enumerables: seq[IntEnumerable]
streams: seq[OutputStream.vtref] streams: seq[OutputStream.vtref]
proc addEnumerable(o: var MyObject, e: IntEnumerable) = proc addEnumerable(o: var MyObject, e: IntEnumerable) =
o.enumerables.add e o.enumerables.add e
proc addStream(o: var MyObject, e: OutputStream.vtref) = proc addStream(o: var MyObject, e: OutputStream.vtref) =
o.streams.add e o.streams.add e
The procs that will be included in the vtable are derived from the concept The procs that will be included in the vtable are derived from the concept
body and include all proc calls for which all param types were specified as body and include all proc calls for which all param types were specified as
concrete types. All such calls should include exactly one param of the type concrete types. All such calls should include exactly one param of the type
matched against the concept (not necessarily in the first position), which matched against the concept (not necessarily in the first position), which
will be considered the value bound to the vtable. will be considered the value bound to the vtable.
Overloads will be created for all captured procs, accepting the vtable type Overloads will be created for all captured procs, accepting the vtable type
in the position of the captured underlying object. in the position of the captured underlying object.
Under these rules, it's possible to obtain a vtable type for a concept with Under these rules, it's possible to obtain a vtable type for a concept with
unbound type parameters or one instantiated with metatypes (type classes), unbound type parameters or one instantiated with metatypes (type classes),
but it will include a smaller number of captured procs. A completely empty but it will include a smaller number of captured procs. A completely empty
vtable will be reported as an error. vtable will be reported as an error.
The ``vtref`` magic produces types which can be bound to ``ref`` types and The ``vtref`` magic produces types which can be bound to ``ref`` types and
the ``vtptr`` magic produced types bound to ``ptr`` types. the ``vtptr`` magic produced types bound to ``ptr`` types.
Symbol lookup in generics Symbol lookup in generics