Mastering Nim: A complete guide to the programming language by Andreas Rumpf

Mastering Nim: A complete guide to the programming language by Andreas Rumpf

Author:Andreas Rumpf [Rumpf, Andreas]
Language: eng
Format: epub, pdf
Published: 2023-08-21T18:30:00+00:00


Operators with one parameter are prefix operators, operators with two parameters are infix operators. (However, the parser distinguishes these from the operator’s position within an expression.) There is no way to

declare postfix operators: all postfix operators are built-in and handled by the grammar explicitly.

Any operator can be called like an ordinary proc with the òpr\` notation.

(Thus an operator can have more than two parameters):

proc `*+`(a, b, c: int): int =

# Multiply and add

result = a * b + c

assert `*+`(3, 4, 6) == `+`(`*`(a, b), c)

21.1. Method call syntax

The syntax obj.methodName(args) can be used instead of methodName(obj,

args). The parentheses can be omitted if there are no remaining arguments: obj.len (instead of len(obj)).

This method call syntax is not restricted to objects, it can be used to supply any type of first argument for routines:

echo "abc" .len # is the same as echo len "abc"

echo "abc" .toUpper()

echo { 'a' , 'b' , 'c' }.card

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.

The method call syntax conflicts with explicit generic instantiations: p[T](x) cannot be written as x.p[T] because x.p[T] is always parsed as (x.p)[T].

See also: Section 27.17, “Method call syntax limitations”.

The [: ] notation was designed to mitigate this issue: x.p[:T] is rewritten by the parser to p[T](x), x.p[:T](y) is rewritten to p[T](x, y). Note that [: ] has no AST representation, the rewrite is performed directly in the parsing step.

147



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.