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.)
what is the built-in $
the built-in $ (stringify) operator turns any basic type into a string, which you can then print to the console using the echo proc.
what is a enum and its features
A variable of an enumeration type can only be assigned one of the enumeration’s specified values.
These values are a set of ordered symbols.
Each symbol is mapped to an integer value internally. The first symbol is represented at runtime by 0, the second by 1 and so on.
what is the ‘type’ statement
the statetement to define a advanced type. As the var statement.
how can you to convert any enumeration value to its name
with the $ operator
how can you can convert any enumeration it to its underlying integer value.
with the ‘orc’ proc
The symbols of enum types can be assigned an explicit ordinal value. How to do that.
he ordinal values must be in ascending order.
A symbol whose ordinal value is not explicitly given is assigned the value of the previous symbol + 1.
what are the Ordinal types
Enumerations without holes, integer types, char and bool (and subranges)
what are the Ordinal types features
ord(x) returns the integer value that is used to represent x’s value
inc(x) increments x by one
inc(x, n) increments x by n; n is an integer
dec(x) decrements x by one
dec(x, n) decrements x by n; n is an integer
succ(x) returns the successor of x
succ(x, n) returns the n’th successor of x
pred(x) returns the predecessor of x
pred(x, n) returns the n’th predecessor of x