Promote "echo" syntax without parenthesis
This commit is contained in:
parent
4246f660ea
commit
cd574d1976
7 changed files with 93 additions and 93 deletions
|
|
@ -15,15 +15,15 @@ Example:
|
||||||
try:
|
try:
|
||||||
var a = readLine(f)
|
var a = readLine(f)
|
||||||
var b = readLine(f)
|
var b = readLine(f)
|
||||||
echo("sum: " & $(parseInt(a) + parseInt(b)))
|
echo "sum: " & $(parseInt(a + parseInt(b)))
|
||||||
except OverflowError:
|
except OverflowError:
|
||||||
echo("overflow!")
|
echo "overflow!"
|
||||||
except ValueError:
|
except ValueError:
|
||||||
echo("could not convert string to integer")
|
echo "could not convert string to integer"
|
||||||
except IOError:
|
except IOError:
|
||||||
echo("IO error!")
|
echo "IO error!"
|
||||||
except:
|
except:
|
||||||
echo("Unknown exception!")
|
echo "Unknown exception!"
|
||||||
finally:
|
finally:
|
||||||
close(f)
|
close(f)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -144,7 +144,7 @@ modules don't need to import a module's dependencies:
|
||||||
|
|
||||||
# B.MyObject has been imported implicitly here:
|
# B.MyObject has been imported implicitly here:
|
||||||
var x: MyObject
|
var x: MyObject
|
||||||
echo($x)
|
echo $x
|
||||||
|
|
||||||
Note on paths
|
Note on paths
|
||||||
-----------
|
-----------
|
||||||
|
|
|
||||||
|
|
@ -129,9 +129,9 @@ to supply any type of first argument for procedures:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
|
|
||||||
echo("abc".len) # is the same as echo(len("abc"))
|
echo "abc".len # is the same as echo len "abc"
|
||||||
echo("abc".toUpper())
|
echo "abc".toUpper()
|
||||||
echo({'a', 'b', 'c'}.card)
|
echo {'a', 'b', 'c'}.card
|
||||||
stdout.writeLine("Hallo") # the same as writeLine(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
|
Another way to look at the method call syntax is that it provides the missing
|
||||||
|
|
@ -486,7 +486,7 @@ state are automatically saved between calls. Example:
|
||||||
inc(i)
|
inc(i)
|
||||||
|
|
||||||
for ch in items("hello world"): # `ch` is an iteration variable
|
for ch in items("hello world"): # `ch` is an iteration variable
|
||||||
echo(ch)
|
echo ch
|
||||||
|
|
||||||
The compiler generates code as if the programmer would have written this:
|
The compiler generates code as if the programmer would have written this:
|
||||||
|
|
||||||
|
|
@ -494,7 +494,7 @@ The compiler generates code as if the programmer would have written this:
|
||||||
var i = 0
|
var i = 0
|
||||||
while i < len(a):
|
while i < len(a):
|
||||||
var ch = a[i]
|
var ch = a[i]
|
||||||
echo(ch)
|
echo ch
|
||||||
inc(i)
|
inc(i)
|
||||||
|
|
||||||
If the iterator yields a tuple, there can be as many iteration variables
|
If the iterator yields a tuple, there can be as many iteration variables
|
||||||
|
|
|
||||||
|
|
@ -234,11 +234,11 @@ Example:
|
||||||
var name = readLine(stdin)
|
var name = readLine(stdin)
|
||||||
|
|
||||||
if name == "Andreas":
|
if name == "Andreas":
|
||||||
echo("What a nice name!")
|
echo "What a nice name!"
|
||||||
elif name == "":
|
elif name == "":
|
||||||
echo("Don't you have a name?")
|
echo "Don't you have a name?"
|
||||||
else:
|
else:
|
||||||
echo("Boring name...")
|
echo "Boring name..."
|
||||||
|
|
||||||
The ``if`` statement is a simple way to make a branch in the control flow:
|
The ``if`` statement is a simple way to make a branch in the control flow:
|
||||||
The expression after the keyword ``if`` is evaluated, if it is true
|
The expression after the keyword ``if`` is evaluated, if it is true
|
||||||
|
|
@ -269,17 +269,17 @@ Example:
|
||||||
|
|
||||||
case readline(stdin)
|
case readline(stdin)
|
||||||
of "delete-everything", "restart-computer":
|
of "delete-everything", "restart-computer":
|
||||||
echo("permission denied")
|
echo "permission denied"
|
||||||
of "go-for-a-walk": echo("please yourself")
|
of "go-for-a-walk": echo "please yourself"
|
||||||
else: echo("unknown command")
|
else: echo "unknown command"
|
||||||
|
|
||||||
# indentation of the branches is also allowed; and so is an optional colon
|
# indentation of the branches is also allowed; and so is an optional colon
|
||||||
# after the selecting expression:
|
# after the selecting expression:
|
||||||
case readline(stdin):
|
case readline(stdin):
|
||||||
of "delete-everything", "restart-computer":
|
of "delete-everything", "restart-computer":
|
||||||
echo("permission denied")
|
echo "permission denied"
|
||||||
of "go-for-a-walk": echo("please yourself")
|
of "go-for-a-walk": echo "please yourself"
|
||||||
else: echo("unknown command")
|
else: echo "unknown command"
|
||||||
|
|
||||||
|
|
||||||
The ``case`` statement is similar to the if statement, but it represents
|
The ``case`` statement is similar to the if statement, but it represents
|
||||||
|
|
@ -326,13 +326,13 @@ Example:
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
|
|
||||||
when sizeof(int) == 2:
|
when sizeof(int) == 2:
|
||||||
echo("running on a 16 bit system!")
|
echo "running on a 16 bit system!"
|
||||||
elif sizeof(int) == 4:
|
elif sizeof(int) == 4:
|
||||||
echo("running on a 32 bit system!")
|
echo "running on a 32 bit system!"
|
||||||
elif sizeof(int) == 8:
|
elif sizeof(int) == 8:
|
||||||
echo("running on a 64 bit system!")
|
echo "running on a 64 bit system!"
|
||||||
else:
|
else:
|
||||||
echo("cannot happen!")
|
echo "cannot happen!"
|
||||||
|
|
||||||
The ``when`` statement is almost identical to the ``if`` statement with some
|
The ``when`` statement is almost identical to the ``if`` statement with some
|
||||||
exceptions:
|
exceptions:
|
||||||
|
|
@ -435,7 +435,7 @@ Example:
|
||||||
if a[j][i] == 7:
|
if a[j][i] == 7:
|
||||||
found = true
|
found = true
|
||||||
break myblock # leave the block, in this case both for-loops
|
break myblock # leave the block, in this case both for-loops
|
||||||
echo(found)
|
echo found
|
||||||
|
|
||||||
The block statement is a means to group statements to a (named) ``block``.
|
The block statement is a means to group statements to a (named) ``block``.
|
||||||
Inside the block, the ``break`` statement is allowed to leave the block
|
Inside the block, the ``break`` statement is allowed to leave the block
|
||||||
|
|
@ -462,10 +462,10 @@ While statement
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
echo("Please tell me your password: \n")
|
echo "Please tell me your password: \n"
|
||||||
var pw = readLine(stdin)
|
var pw = readLine(stdin)
|
||||||
while pw != "12345":
|
while pw != "12345":
|
||||||
echo("Wrong password! Next try: \n")
|
echo "Wrong password! Next try: \n"
|
||||||
pw = readLine(stdin)
|
pw = readLine(stdin)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -546,7 +546,7 @@ so that the builtin ``echo`` proc does what is expected:
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
proc echo*(x: varargs[expr, `$`]) {...}
|
proc echo*(x: varargs[expr, `$`]) {...}
|
||||||
|
|
||||||
echo(@[1, 2, 3])
|
echo @[1, 2, 3]
|
||||||
# prints "@[1, 2, 3]" and not "123"
|
# prints "@[1, 2, 3]" and not "123"
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
124
doc/tut1.txt
124
doc/tut1.txt
|
|
@ -31,9 +31,9 @@ We start the tour with a modified "hello world" program:
|
||||||
|
|
||||||
.. code-block:: Nim
|
.. code-block:: Nim
|
||||||
# This is a comment
|
# This is a comment
|
||||||
echo("What's your name? ")
|
echo "What's your name? "
|
||||||
var name: string = readLine(stdin)
|
var name: string = readLine(stdin)
|
||||||
echo("Hi, ", name, "!")
|
echo "Hi, ", name, "!"
|
||||||
|
|
||||||
|
|
||||||
Save this code to the file "greetings.nim". Now compile and run it::
|
Save this code to the file "greetings.nim". Now compile and run it::
|
||||||
|
|
@ -250,11 +250,11 @@ The if statement is one way to branch the control flow:
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
let name = readLine(stdin)
|
let name = readLine(stdin)
|
||||||
if name == "":
|
if name == "":
|
||||||
echo("Poor soul, you lost your name?")
|
echo "Poor soul, you lost your name?"
|
||||||
elif name == "name":
|
elif name == "name":
|
||||||
echo("Very funny, your name is name.")
|
echo "Very funny, your name is name."
|
||||||
else:
|
else:
|
||||||
echo("Hi, ", name, "!")
|
echo "Hi, ", name, "!"
|
||||||
|
|
||||||
There can be zero or more ``elif`` parts, and the ``else`` part is optional.
|
There can be zero or more ``elif`` parts, and the ``else`` part is optional.
|
||||||
The keyword ``elif`` is short for ``else if``, and is useful to avoid
|
The keyword ``elif`` is short for ``else if``, and is useful to avoid
|
||||||
|
|
@ -272,13 +272,13 @@ a multi-branch:
|
||||||
let name = readLine(stdin)
|
let name = readLine(stdin)
|
||||||
case name
|
case name
|
||||||
of "":
|
of "":
|
||||||
echo("Poor soul, you lost your name?")
|
echo "Poor soul, you lost your name?"
|
||||||
of "name":
|
of "name":
|
||||||
echo("Very funny, your name is name.")
|
echo "Very funny, your name is name."
|
||||||
of "Dave", "Frank":
|
of "Dave", "Frank":
|
||||||
echo("Cool name!")
|
echo "Cool name!"
|
||||||
else:
|
else:
|
||||||
echo("Hi, ", name, "!")
|
echo "Hi, ", name, "!"
|
||||||
|
|
||||||
As it can be seen, for an ``of`` branch a comma separated list of values is also
|
As it can be seen, for an ``of`` branch a comma separated list of values is also
|
||||||
allowed.
|
allowed.
|
||||||
|
|
@ -291,11 +291,11 @@ For integers or other ordinal types value ranges are also possible:
|
||||||
# this statement will be explained later:
|
# this statement will be explained later:
|
||||||
from strutils import parseInt
|
from strutils import parseInt
|
||||||
|
|
||||||
echo("A number please: ")
|
echo "A number please: "
|
||||||
let n = parseInt(readLine(stdin))
|
let n = parseInt(readLine(stdin))
|
||||||
case n
|
case n
|
||||||
of 0..2, 4..7: echo("The number is in the set: {0, 1, 2, 4, 5, 6, 7}")
|
of 0..2, 4..7: echo "The number is in the set: {0, 1, 2, 4, 5, 6, 7}"
|
||||||
of 3, 8: echo("The number is 3 or 8")
|
of 3, 8: echo "The number is 3 or 8"
|
||||||
|
|
||||||
However, the above code does not compile: the reason is that you have to cover
|
However, the above code does not compile: the reason is that you have to cover
|
||||||
every value that ``n`` may contain, but the code only handles the values
|
every value that ``n`` may contain, but the code only handles the values
|
||||||
|
|
@ -306,8 +306,8 @@ the compiler that for every other value nothing should be done:
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
...
|
...
|
||||||
case n
|
case n
|
||||||
of 0..2, 4..7: echo("The number is in the set: {0, 1, 2, 4, 5, 6, 7}")
|
of 0..2, 4..7: echo "The number is in the set: {0, 1, 2, 4, 5, 6, 7}"
|
||||||
of 3, 8: echo("The number is 3 or 8")
|
of 3, 8: echo "The number is 3 or 8"
|
||||||
else: discard
|
else: discard
|
||||||
|
|
||||||
The empty `discard statement`_ is a *do nothing* statement. The compiler knows
|
The empty `discard statement`_ is a *do nothing* statement. The compiler knows
|
||||||
|
|
@ -327,10 +327,10 @@ The while statement is a simple looping construct:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
|
|
||||||
echo("What's your name? ")
|
echo "What's your name? "
|
||||||
var name = readLine(stdin)
|
var name = readLine(stdin)
|
||||||
while name == "":
|
while name == "":
|
||||||
echo("Please tell me your name: ")
|
echo "Please tell me your name: "
|
||||||
name = readLine(stdin)
|
name = readLine(stdin)
|
||||||
# no ``var``, because we do not declare a new variable here
|
# no ``var``, because we do not declare a new variable here
|
||||||
|
|
||||||
|
|
@ -346,9 +346,9 @@ provides. The example uses the built-in `countup <system.html#countup>`_
|
||||||
iterator:
|
iterator:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
echo("Counting to ten: ")
|
echo "Counting to ten: "
|
||||||
for i in countup(1, 10):
|
for i in countup(1, 10):
|
||||||
echo($i)
|
echo $i
|
||||||
# --> Outputs 1 2 3 4 5 6 7 8 9 10 on different lines
|
# --> Outputs 1 2 3 4 5 6 7 8 9 10 on different lines
|
||||||
|
|
||||||
The built-in `$ <system.html#$>`_ operator turns an integer (``int``) and many
|
The built-in `$ <system.html#$>`_ operator turns an integer (``int``) and many
|
||||||
|
|
@ -358,19 +358,19 @@ other types into a string. The variable ``i`` is implicitly declared by the
|
||||||
Each value is ``echo``-ed. This code does the same:
|
Each value is ``echo``-ed. This code does the same:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
echo("Counting to 10: ")
|
echo "Counting to 10: "
|
||||||
var i = 1
|
var i = 1
|
||||||
while i <= 10:
|
while i <= 10:
|
||||||
echo($i)
|
echo $i
|
||||||
inc(i) # increment i by 1
|
inc(i) # increment i by 1
|
||||||
# --> Outputs 1 2 3 4 5 6 7 8 9 10 on different lines
|
# --> Outputs 1 2 3 4 5 6 7 8 9 10 on different lines
|
||||||
|
|
||||||
Counting down can be achieved as easily (but is less often needed):
|
Counting down can be achieved as easily (but is less often needed):
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
echo("Counting down from 10 to 1: ")
|
echo "Counting down from 10 to 1: "
|
||||||
for i in countdown(10, 1):
|
for i in countdown(10, 1):
|
||||||
echo($i)
|
echo $i
|
||||||
# --> Outputs 10 9 8 7 6 5 4 3 2 1 on different lines
|
# --> Outputs 10 9 8 7 6 5 4 3 2 1 on different lines
|
||||||
|
|
||||||
Since counting up occurs so often in programs, Nim also has a `..
|
Since counting up occurs so often in programs, Nim also has a `..
|
||||||
|
|
@ -390,7 +390,7 @@ outside the loop:
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
while false:
|
while false:
|
||||||
var x = "hi"
|
var x = "hi"
|
||||||
echo(x) # does not work
|
echo x # does not work
|
||||||
|
|
||||||
A while (for) statement introduces an implicit block. Identifiers
|
A while (for) statement introduces an implicit block. Identifiers
|
||||||
are only visible within the block they have been declared. The ``block``
|
are only visible within the block they have been declared. The ``block``
|
||||||
|
|
@ -399,7 +399,7 @@ statement can be used to open a new block explicitly:
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
block myblock:
|
block myblock:
|
||||||
var x = "hi"
|
var x = "hi"
|
||||||
echo(x) # does not work either
|
echo x # does not work either
|
||||||
|
|
||||||
The block's *label* (``myblock`` in the example) is optional.
|
The block's *label* (``myblock`` in the example) is optional.
|
||||||
|
|
||||||
|
|
@ -412,18 +412,18 @@ innermost construct, unless a label of a block is given:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
block myblock:
|
block myblock:
|
||||||
echo("entering block")
|
echo "entering block"
|
||||||
while true:
|
while true:
|
||||||
echo("looping")
|
echo "looping"
|
||||||
break # leaves the loop, but not the block
|
break # leaves the loop, but not the block
|
||||||
echo("still in block")
|
echo "still in block"
|
||||||
|
|
||||||
block myblock2:
|
block myblock2:
|
||||||
echo("entering block")
|
echo "entering block"
|
||||||
while true:
|
while true:
|
||||||
echo("looping")
|
echo "looping"
|
||||||
break myblock2 # leaves the block (and the loop)
|
break myblock2 # leaves the block (and the loop)
|
||||||
echo("still in block")
|
echo "still in block"
|
||||||
|
|
||||||
|
|
||||||
Continue statement
|
Continue statement
|
||||||
|
|
@ -435,7 +435,7 @@ the next iteration immediately:
|
||||||
while true:
|
while true:
|
||||||
let x = readLine(stdin)
|
let x = readLine(stdin)
|
||||||
if x == "": continue
|
if x == "": continue
|
||||||
echo(x)
|
echo x
|
||||||
|
|
||||||
|
|
||||||
When statement
|
When statement
|
||||||
|
|
@ -446,13 +446,13 @@ Example:
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
|
|
||||||
when system.hostOS == "windows":
|
when system.hostOS == "windows":
|
||||||
echo("running on Windows!")
|
echo "running on Windows!"
|
||||||
elif system.hostOS == "linux":
|
elif system.hostOS == "linux":
|
||||||
echo("running on Linux!")
|
echo "running on Linux!"
|
||||||
elif system.hostOS == "macosx":
|
elif system.hostOS == "macosx":
|
||||||
echo("running on Mac OS X!")
|
echo "running on Mac OS X!"
|
||||||
else:
|
else:
|
||||||
echo("unknown operating system")
|
echo "unknown operating system"
|
||||||
|
|
||||||
The ``when`` statement is almost identical to the ``if`` statement with some
|
The ``when`` statement is almost identical to the ``if`` statement with some
|
||||||
differences:
|
differences:
|
||||||
|
|
@ -533,17 +533,17 @@ procedures are defined with the ``proc`` keyword:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
proc yes(question: string): bool =
|
proc yes(question: string): bool =
|
||||||
echo(question, " (y/n)")
|
echo question, " (y/n)"
|
||||||
while true:
|
while true:
|
||||||
case readLine(stdin)
|
case readLine(stdin)
|
||||||
of "y", "Y", "yes", "Yes": return true
|
of "y", "Y", "yes", "Yes": return true
|
||||||
of "n", "N", "no", "No": return false
|
of "n", "N", "no", "No": return false
|
||||||
else: echo("Please be clear: yes or no")
|
else: echo "Please be clear: yes or no"
|
||||||
|
|
||||||
if yes("Should I delete all your important files?"):
|
if yes("Should I delete all your important files?"):
|
||||||
echo("I'm sorry Dave, I'm afraid I can't do that.")
|
echo "I'm sorry Dave, I'm afraid I can't do that."
|
||||||
else:
|
else:
|
||||||
echo("I think you know what the problem is just as well as I do.")
|
echo "I think you know what the problem is just as well as I do."
|
||||||
|
|
||||||
This example shows a procedure named ``yes`` that asks the user a ``question``
|
This example shows a procedure named ``yes`` that asks the user a ``question``
|
||||||
and returns true if they answered "yes" (or something similar) and returns
|
and returns true if they answered "yes" (or something similar) and returns
|
||||||
|
|
@ -611,8 +611,8 @@ caller, a ``var`` parameter can be used:
|
||||||
var
|
var
|
||||||
x, y: int
|
x, y: int
|
||||||
divmod(8, 5, x, y) # modifies x and y
|
divmod(8, 5, x, y) # modifies x and y
|
||||||
echo(x)
|
echo x
|
||||||
echo(y)
|
echo y
|
||||||
|
|
||||||
In the example, ``res`` and ``remainder`` are `var parameters`.
|
In the example, ``res`` and ``remainder`` are `var parameters`.
|
||||||
Var parameters can be modified by the procedure and the changes are
|
Var parameters can be modified by the procedure and the changes are
|
||||||
|
|
@ -701,8 +701,8 @@ Nim provides the ability to overload procedures similar to C++:
|
||||||
if x: result = "true"
|
if x: result = "true"
|
||||||
else: result = "false"
|
else: result = "false"
|
||||||
|
|
||||||
echo(toString(13)) # calls the toString(x: int) proc
|
echo toString(13) # calls the toString(x: int) proc
|
||||||
echo(toString(true)) # calls the toString(x: bool) proc
|
echo toString(true) # calls the toString(x: bool) proc
|
||||||
|
|
||||||
(Note that ``toString`` is usually the `$ <system.html#$>`_ operator in
|
(Note that ``toString`` is usually the `$ <system.html#$>`_ operator in
|
||||||
Nim.) The compiler chooses the most appropriate proc for the ``toString``
|
Nim.) The compiler chooses the most appropriate proc for the ``toString``
|
||||||
|
|
@ -743,7 +743,7 @@ The "``" notation can also be used to call an operator just like any other
|
||||||
procedure:
|
procedure:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
if `==`( `+`(3, 4), 7): echo("True")
|
if `==`( `+`(3, 4), 7): echo "True"
|
||||||
|
|
||||||
|
|
||||||
Forward declarations
|
Forward declarations
|
||||||
|
|
@ -790,9 +790,9 @@ Iterators
|
||||||
Let's return to the boring counting example:
|
Let's return to the boring counting example:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
echo("Counting to ten: ")
|
echo "Counting to ten: "
|
||||||
for i in countup(1, 10):
|
for i in countup(1, 10):
|
||||||
echo($i)
|
echo $i
|
||||||
|
|
||||||
Can a `countup <system.html#countup>`_ proc be written that supports this
|
Can a `countup <system.html#countup>`_ proc be written that supports this
|
||||||
loop? Lets try:
|
loop? Lets try:
|
||||||
|
|
@ -1000,15 +1000,15 @@ there is a difference between the ``$`` and ``repr`` outputs:
|
||||||
myString = "nim"
|
myString = "nim"
|
||||||
myInteger = 42
|
myInteger = 42
|
||||||
myFloat = 3.14
|
myFloat = 3.14
|
||||||
echo($myBool, ":", repr(myBool))
|
echo $myBool, ":", repr(myBool)
|
||||||
# --> true:true
|
# --> true:true
|
||||||
echo($myCharacter, ":", repr(myCharacter))
|
echo $myCharacter, ":", repr(myCharacter)
|
||||||
# --> n:'n'
|
# --> n:'n'
|
||||||
echo($myString, ":", repr(myString))
|
echo $myString, ":", repr(myString)
|
||||||
# --> nim:0x10fa8c050"nim"
|
# --> nim:0x10fa8c050"nim"
|
||||||
echo($myInteger, ":", repr(myInteger))
|
echo $myInteger, ":", repr(myInteger)
|
||||||
# --> 42:42
|
# --> 42:42
|
||||||
echo($myFloat, ":", repr(myFloat))
|
echo $myFloat, ":", repr(myFloat)
|
||||||
# --> 3.1400000000000001e+00:3.1400000000000001e+00
|
# --> 3.1400000000000001e+00:3.1400000000000001e+00
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1040,7 +1040,7 @@ at runtime by 0, the second by 1 and so on. Example:
|
||||||
north, east, south, west
|
north, east, south, west
|
||||||
|
|
||||||
var x = south # `x` is of type `Direction`; its value is `south`
|
var x = south # `x` is of type `Direction`; its value is `south`
|
||||||
echo($x) # writes "south" to `stdout`
|
echo $x # writes "south" to `stdout`
|
||||||
|
|
||||||
All comparison operators can be used with enumeration types.
|
All comparison operators can be used with enumeration types.
|
||||||
|
|
||||||
|
|
@ -1133,7 +1133,7 @@ Arrays can be constructed via ``[]``:
|
||||||
x: IntArray
|
x: IntArray
|
||||||
x = [1, 2, 3, 4, 5, 6]
|
x = [1, 2, 3, 4, 5, 6]
|
||||||
for i in low(x)..high(x):
|
for i in low(x)..high(x):
|
||||||
echo(x[i])
|
echo x[i]
|
||||||
|
|
||||||
The notation ``x[i]`` is used to access the i-th element of ``x``.
|
The notation ``x[i]`` is used to access the i-th element of ``x``.
|
||||||
Array access is always bounds checked (at compile-time or at runtime). These
|
Array access is always bounds checked (at compile-time or at runtime). These
|
||||||
|
|
@ -1208,7 +1208,7 @@ to specify a range from zero to the specified index minus one:
|
||||||
x = [1, 2, 3, 4, 5, 6]
|
x = [1, 2, 3, 4, 5, 6]
|
||||||
y = x
|
y = x
|
||||||
for i in low(x)..high(x):
|
for i in low(x)..high(x):
|
||||||
echo(x[i], y[i])
|
echo x[i], y[i]
|
||||||
|
|
||||||
|
|
||||||
Sequences
|
Sequences
|
||||||
|
|
@ -1254,13 +1254,13 @@ value. Here the ``for`` statement is looping over the results from the
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
for i in @[3, 4, 5]:
|
for i in @[3, 4, 5]:
|
||||||
echo($i)
|
echo $i
|
||||||
# --> 3
|
# --> 3
|
||||||
# --> 4
|
# --> 4
|
||||||
# --> 5
|
# --> 5
|
||||||
|
|
||||||
for i, value in @[3, 4, 5]:
|
for i, value in @[3, 4, 5]:
|
||||||
echo("index: ", $i, ", value:", $value)
|
echo "index: ", $i, ", value:", $value
|
||||||
# --> index: 0, value:3
|
# --> index: 0, value:3
|
||||||
# --> index: 1, value:4
|
# --> index: 1, value:4
|
||||||
# --> index: 2, value:5
|
# --> index: 2, value:5
|
||||||
|
|
@ -1386,16 +1386,16 @@ integer.
|
||||||
# the same, but less readable:
|
# the same, but less readable:
|
||||||
person = ("Peter", 30)
|
person = ("Peter", 30)
|
||||||
|
|
||||||
echo(person.name) # "Peter"
|
echo person.name # "Peter"
|
||||||
echo(person.age) # 30
|
echo person.age # 30
|
||||||
|
|
||||||
echo(person[0]) # "Peter"
|
echo person[0] # "Peter"
|
||||||
echo(person[1]) # 30
|
echo person[1] # 30
|
||||||
|
|
||||||
# You don't need to declare tuples in a separate type section.
|
# You don't need to declare tuples in a separate type section.
|
||||||
var building: tuple[street: string, number: int]
|
var building: tuple[street: string, number: int]
|
||||||
building = ("Rue del Percebe", 13)
|
building = ("Rue del Percebe", 13)
|
||||||
echo(building.street)
|
echo building.street
|
||||||
|
|
||||||
# The following line does not compile, they are different tuples!
|
# The following line does not compile, they are different tuples!
|
||||||
#person = building
|
#person = building
|
||||||
|
|
@ -1503,7 +1503,7 @@ techniques.
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
proc echoItem(x: int) = echo(x)
|
proc echoItem(x: int) = echo x
|
||||||
|
|
||||||
proc forEach(action: proc (x: int)) =
|
proc forEach(action: proc (x: int)) =
|
||||||
const
|
const
|
||||||
|
|
|
||||||
|
|
@ -204,9 +204,9 @@ for any type:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
|
|
||||||
echo("abc".len) # is the same as echo(len("abc"))
|
echo "abc".len # is the same as echo len("abc")
|
||||||
echo("abc".toUpper())
|
echo "abc".toUpper()
|
||||||
echo({'a', 'b', 'c'}.card)
|
echo {'a', 'b', 'c'}.card
|
||||||
stdout.writeLine("Hallo") # the same as writeLine(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
|
(Another way to look at the method call syntax is that it provides the missing
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue