Tutorial Flashcards
which are pairs and mpairs
it’s a iterator which provides the element and an index number (immutable and mutable respectively)
items and mitems
it’s a iterator which provides immutable and mutable elements respectively
Control flow statements scope
is not accessible from outside, is a new scope
what is block?
The block statement can be used to open a new block explicitly
A block can be left prematurely with a
break statement
what’s the purpouse Continue statement
Like in many other programming languages, a continue statement starts the next iteration immediately
The when statement is almost identical to the if statement, but with these differences:
Each condition must be a constant expression since it is evaluated by the compiler.
The statements within a branch do not open a new scope.
The compiler checks the semantics and produces code only for the statements that belong to the first condition that evaluates to true.
What is the utility of when statement
The when statement is useful for writing platform specific code, similar to the #ifdef construct in the C programming language.
How to define a function
the concept of a procedure is needed. proc name(arg: type): typeOfReturn =
whait is the Result variable
A procedure that returns a value has an implicit result variable declared that represents the return value.
A return statement with no expression is a shorthand for return result.
The result value is always returned automatically at the end of a procedure if there is no return statement at the exit.
How to assign params by reference
a var parameter can be used proc divmod(a, b: int; res, remainder: var int) = res = a div b # integer division remainder = a mod b # integer modulo operation
What is the discard statement on the procedures definition
To call a procedure that returns a value just for its side effects and ignoring its return value, a discard statement must be used.
The return value can be ignored implicitly if the called proc/iterator has been declared with the discardable pragma
What is the {.discardable.}
The return value can be ignored implicitly if the called proc/iterator has been declared with the discardable pragma
How to define a new operator
enclose the operator in backticks “``”:
For example
proc $
(x: myDataType): string = …
# now the $ operator also works with myDataType, overloading resolution
How to do a Forward declarations
The syntax for such a forward declaration is simple: just omit the = and the procedure’s body.
Iterators look very similar to procedures, but there are several important differences
Iterators can only be called from ‘for’ loops.
Iterators cannot contain a return statement (and procs cannot contain a yield statement).
Iterators have no implicit result variable.
Iterators do not support recursion.
Iterators cannot be forward declared, because the compiler must be able to inline an iterator. (This restriction will be gone in a future version of the compiler.)