branching: if when case
TLDR
- new scopes are introduced immediately aftered the keywords (e.g. if, elif, etc)
- except for when statements/expressions
- use whens for completely removing sections of code at compile time
- all can be used as expressions and the result assigned to a var
- case statement branches should be listed in order of most expected
branching TODOs
- likely
- unlikely
- an example of using when (and if?) inside an object constructor
- there are examples in the doc where when is used to optionally define props
- e.g. this file: https://github.com/nim-lang/Nim/blob/devel/lib/std/private/threadtypes.nim
when
- a compile time if statement
- the condition MUST be a constant expression
- does not open a new scope
- only the first truthy value is compiled
case
- similar to if, but represents a multi-branch selection
- supports strings, ordinal types and integers
- ints/ordinals can also use ranges
- the of part must match the value type
- can also use elif, else branches
Procs
proc positiveOrNegative(num: int): string {....raises: [], tags: [], forbids: [].}
- Source Edit