Use ref objects with inheritance in Tut2 (fixes #1817)
This commit is contained in:
parent
e680e43fc2
commit
748a7b866f
1 changed files with 12 additions and 10 deletions
20
doc/tut2.txt
20
doc/tut2.txt
|
|
@ -56,11 +56,11 @@ Objects have access to their type at runtime. There is an
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
type
|
type
|
||||||
Person = object of RootObj
|
Person = ref object of RootObj
|
||||||
name*: string # the * means that `name` is accessible from other modules
|
name*: string # the * means that `name` is accessible from other modules
|
||||||
age: int # no * means that the field is hidden from other modules
|
age: int # no * means that the field is hidden from other modules
|
||||||
|
|
||||||
Student = object of Person # Student inherits from Person
|
Student = ref object of Person # Student inherits from Person
|
||||||
id: int # with an id field
|
id: int # with an id field
|
||||||
|
|
||||||
var
|
var
|
||||||
|
|
@ -69,6 +69,7 @@ Objects have access to their type at runtime. There is an
|
||||||
assert(student of Student) # is true
|
assert(student of Student) # is true
|
||||||
# object construction:
|
# object construction:
|
||||||
student = Student(name: "Anton", age: 5, id: 2)
|
student = Student(name: "Anton", age: 5, id: 2)
|
||||||
|
echo student[]
|
||||||
|
|
||||||
Object fields that should be visible from outside the defining module have to
|
Object fields that should be visible from outside the defining module have to
|
||||||
be marked by ``*``. In contrast to tuples, different object types are
|
be marked by ``*``. In contrast to tuples, different object types are
|
||||||
|
|
@ -228,7 +229,7 @@ is needed:
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
|
|
||||||
type
|
type
|
||||||
Socket* = object of RootObj
|
Socket* = ref object of RootObj
|
||||||
FHost: int # cannot be accessed from the outside of the module
|
FHost: int # cannot be accessed from the outside of the module
|
||||||
# the `F` prefix is a convention to avoid clashes since
|
# the `F` prefix is a convention to avoid clashes since
|
||||||
# the accessors are named `host`
|
# the accessors are named `host`
|
||||||
|
|
@ -241,8 +242,8 @@ is needed:
|
||||||
## getter of hostAddr
|
## getter of hostAddr
|
||||||
s.FHost
|
s.FHost
|
||||||
|
|
||||||
var
|
var s: Socket
|
||||||
s: Socket
|
new s
|
||||||
s.host = 34 # same as `host=`(s, 34)
|
s.host = 34 # same as `host=`(s, 34)
|
||||||
|
|
||||||
(The example also shows ``inline`` procedures.)
|
(The example also shows ``inline`` procedures.)
|
||||||
|
|
@ -313,8 +314,8 @@ dispatching:
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
|
|
||||||
type
|
type
|
||||||
Thing = object of RootObj
|
Thing = ref object of RootObj
|
||||||
Unit = object of Thing
|
Unit = ref object of Thing
|
||||||
x: int
|
x: int
|
||||||
|
|
||||||
method collide(a, b: Thing) {.inline.} =
|
method collide(a, b: Thing) {.inline.} =
|
||||||
|
|
@ -326,8 +327,9 @@ dispatching:
|
||||||
method collide(a: Unit, b: Thing) {.inline.} =
|
method collide(a: Unit, b: Thing) {.inline.} =
|
||||||
echo "2"
|
echo "2"
|
||||||
|
|
||||||
var
|
var a, b: Unit
|
||||||
a, b: Unit
|
new a
|
||||||
|
new b
|
||||||
collide(a, b) # output: 2
|
collide(a, b) # output: 2
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue