Also use ref objects with inheritance in manual
This commit is contained in:
parent
91ed170523
commit
f8472402d5
2 changed files with 23 additions and 22 deletions
|
|
@ -121,7 +121,7 @@ different; for this a special setter syntax 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`
|
||||||
|
|
@ -134,8 +134,8 @@ different; for this a special setter syntax 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)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -351,32 +351,32 @@ dispatch.
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
type
|
type
|
||||||
Expression = object of RootObj ## abstract base class for an expression
|
Expression = ref object of RootObj ## abstract base class for an expression
|
||||||
Literal = object of Expression
|
Literal = ref object of Expression
|
||||||
x: int
|
x: int
|
||||||
PlusExpr = object of Expression
|
PlusExpr = ref object of Expression
|
||||||
a, b: ref Expression
|
a, b: Expression
|
||||||
|
|
||||||
method eval(e: ref Expression): int =
|
method eval(e: Expression): int =
|
||||||
# override this base method
|
# override this base method
|
||||||
quit "to override!"
|
quit "to override!"
|
||||||
|
|
||||||
method eval(e: ref Literal): int = return e.x
|
method eval(e: Literal): int = return e.x
|
||||||
|
|
||||||
method eval(e: ref PlusExpr): int =
|
method eval(e: PlusExpr): int =
|
||||||
# watch out: relies on dynamic binding
|
# watch out: relies on dynamic binding
|
||||||
result = eval(e.a) + eval(e.b)
|
result = eval(e.a) + eval(e.b)
|
||||||
|
|
||||||
proc newLit(x: int): ref Literal =
|
proc newLit(x: int): Literal =
|
||||||
new(result)
|
new(result)
|
||||||
result.x = x
|
result.x = x
|
||||||
|
|
||||||
proc newPlus(a, b: ref Expression): ref PlusExpr =
|
proc newPlus(a, b: Expression): PlusExpr =
|
||||||
new(result)
|
new(result)
|
||||||
result.a = a
|
result.a = a
|
||||||
result.b = b
|
result.b = b
|
||||||
|
|
||||||
echo eval(newPlus(newPlus(newLit(1), newLit(2)), newLit(4)))
|
echo eval(newPlus(newPlus(newLit(1), newLit(2)), newLit(4)))
|
||||||
|
|
||||||
In the example the constructors ``newLit`` and ``newPlus`` are procs
|
In the example the constructors ``newLit`` and ``newPlus`` are procs
|
||||||
because they should use static binding, but ``eval`` is a method because it
|
because they should use static binding, but ``eval`` is a method because it
|
||||||
|
|
@ -387,8 +387,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.} =
|
||||||
|
|
@ -400,8 +400,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
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -568,7 +568,7 @@ the ``of`` operator can be used to determine the object's type.
|
||||||
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
|
age: int # no * means that the field is hidden
|
||||||
|
|
||||||
Student = object of Person # a student is a person
|
Student = ref object of Person # a student is a person
|
||||||
id: int # with an id field
|
id: int # with an id field
|
||||||
|
|
||||||
var
|
var
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue