diff options
Diffstat (limited to 'doc/tut2.txt')
-rw-r--r-- | doc/tut2.txt | 40 |
1 files changed, 19 insertions, 21 deletions
diff --git a/doc/tut2.txt b/doc/tut2.txt index e1ac20074..db9e4cd58 100644 --- a/doc/tut2.txt +++ b/doc/tut2.txt @@ -207,7 +207,7 @@ for any type: echo("abc".len) # is the same as echo(len("abc")) echo("abc".toUpper()) echo({'a', 'b', 'c'}.card) - stdout.writeln("Hallo") # the same as writeln(stdout, "Hallo") + stdout.writeLine("Hallo") # the same as writeLine(stdout, "Hallo") (Another way to look at the method call syntax is that it provides the missing postfix notation.) @@ -215,11 +215,11 @@ postfix notation.) So "pure object oriented" code is easy to write: .. code-block:: nim - import strutils + import strutils, sequtils - stdout.writeln("Give a list of numbers (separated by spaces): ") + stdout.writeLine("Give a list of numbers (separated by spaces): ") stdout.write(stdin.readLine.split.map(parseInt).max.`$`) - stdout.writeln(" is the maximum!") + stdout.writeLine(" is the maximum!") Properties @@ -233,17 +233,15 @@ is needed: type Socket* = ref object of RootObj - FHost: int # cannot be accessed from the outside of the module - # the `F` prefix is a convention to avoid clashes since - # the accessors are named `host` + host: int # cannot be accessed from the outside of the module due to missing star proc `host=`*(s: var Socket, value: int) {.inline.} = - ## setter of hostAddr - s.FHost = value + ## setter of host address + s.host = value proc host*(s: Socket): int {.inline.} = - ## getter of hostAddr - s.FHost + ## getter of host address + s.host var s: Socket new s @@ -537,7 +535,7 @@ containers: add(root, newNode("hello")) # instantiates ``newNode`` and ``add`` add(root, "world") # instantiates the second ``add`` proc for str in preorder(root): - stdout.writeln(str) + stdout.writeLine(str) The example shows a generic binary tree. Depending on context, the brackets are used either to introduce type parameters or to instantiate a generic proc, @@ -582,7 +580,7 @@ simple proc for logging: debug = true proc log(msg: string) {.inline.} = - if debug: stdout.writeln(msg) + if debug: stdout.writeLine(msg) var x = 4 @@ -599,7 +597,7 @@ Turning the ``log`` proc into a template solves this problem: debug = true template log(msg: string) = - if debug: stdout.writeln(msg) + if debug: stdout.writeLine(msg) var x = 4 @@ -629,10 +627,10 @@ via a special ``:`` syntax: quit("cannot open: " & fn) withFile(txt, "ttempl3.txt", fmWrite): - txt.writeln("line 1") - txt.writeln("line 2") + txt.writeLine("line 1") + txt.writeLine("line 2") -In the example the two ``writeln`` statements are bound to the ``body`` +In the example the two ``writeLine`` statements are bound to the ``body`` parameter. The ``withFile`` template contains boilerplate code and helps to avoid a common bug: to forget to close the file. Note how the ``let fn = filename`` statement ensures that ``filename`` is evaluated only @@ -686,7 +684,7 @@ variable number of arguments: # add a call to the statement list that writes ": " result.add(newCall("write", newIdentNode("stdout"), newStrLitNode(": "))) # add a call to the statement list that writes the expressions value: - result.add(newCall("writeln", newIdentNode("stdout"), n[i])) + result.add(newCall("writeLine", newIdentNode("stdout"), n[i])) var a: array[0..10, int] @@ -701,15 +699,15 @@ The macro call expands to: .. code-block:: nim write(stdout, "a[0]") write(stdout, ": ") - writeln(stdout, a[0]) + writeLine(stdout, a[0]) write(stdout, "a[1]") write(stdout, ": ") - writeln(stdout, a[1]) + writeLine(stdout, a[1]) write(stdout, "x") write(stdout, ": ") - writeln(stdout, x) + writeLine(stdout, x) |