diff --git a/doc/tut1.rst b/doc/tut1.rst index a7f1b741a..161b4b904 100644 --- a/doc/tut1.rst +++ b/doc/tut1.rst @@ -1441,31 +1441,111 @@ string that is "useless" and replace it with "useful". Note: alternate ways of writing this are ``b[^8..^2] = "useful"`` or as ``b[11..b.len-2] = "useful"`` or as ``b[11.. Error: type mismatch: got (tuple[street: string, number: int]) # but expected 'Person' - # The following works because the field names and types are the same. - var teacher: tuple[name: string, age: int] = ("Mark", 42) - person = teacher - Even though you don't need to declare a type for a tuple to use it, tuples created with different field names will be considered different objects despite having the same field types. @@ -1519,6 +1595,8 @@ variables! For example: echo badname echo badext +Fields of tuples are always public, they don't need to be explicity +marked to be exported, unlike for example fields in an object type. Reference and pointer types --------------------------- diff --git a/doc/tut2.rst b/doc/tut2.rst index d0c6e7247..4c1ec5747 100644 --- a/doc/tut2.rst +++ b/doc/tut2.rst @@ -40,18 +40,16 @@ and more efficient code. In particular, preferring composition over inheritance is often the better design. -Objects -------- +Inheritance +----------- -Like tuples, objects are a means to pack different values together in a -structured way. However, objects provide many features that tuples do not: -They provide inheritance and information hiding. Because objects encapsulate -data, the ``T()`` object constructor should only be used internally and the -programmer should provide a proc to initialize the object (this is called -a *constructor*). - -Objects have access to their type at runtime. There is an -``of`` operator that can be used to check the object's type: +Inheritance in Nim is entirely optional. To enable inheritance with +runtime type information the object needs to inherit from +``RootObj``. This can be done directly, or indirectly by +inheriting from an object that inherits from ``RootObj``. Usually +types with inheritance are also marked as ``ref`` types even though +this isn't strictly enforced. To check at runtime if an object is of a certain +type, the ``of`` operator can be used. .. code-block:: nim :test: "nim c $1" @@ -71,11 +69,6 @@ Objects have access to their type at runtime. There is an student = Student(name: "Anton", age: 5, id: 2) echo student[] -Object fields that should be visible from outside the defining module have to -be marked by ``*``. In contrast to tuples, different object types are -never *equivalent*. New object types can only be defined within a type -section. - Inheritance is done with the ``object of`` syntax. Multiple inheritance is currently not supported. If an object type has no suitable ancestor, ``RootObj`` can be used as its ancestor, but this is only a convention. Objects that have