TalkTalk uses braces around blocks of code, starts a new statement on each line, and uses // for comments. If the last line of a block produces a value, that value becomes the result of the block.
Statements and blocks
A newline normally ends a statement. Semicolons are accepted but frowned upon (and the formatter will probably get rid of them anyway):
Declarations, conditionals, loops, closures, and matches use braces:
A function or closure returns its final expression implicitly:
Use return for an early return.
Comments
// comments run to the end of the line:
Control flow
if is an expression when both branches produce compatible values:
else if chains are supported. A statement-position if may omit else.
loop is either infinite or while-like:
Use break and continue inside loops. for works through the Iterable and Iterator protocols:
0..<3 is half-open; 0..3 is closed.
Calls and member access
Calls use parentheses. Parameters without type annotations are positional, so this function is called as repeat("ha", 3):
The compiler learns from the body that text is a String and count is an Int. You can add those types yourself; typed parameters use their names as call-site labels by default:
Use _ to leave a parameter positional, or write a different label before its local name:
Bindings and Functions covers labels and type inference in more detail.
Member access uses ., tuple fields use numeric members, and subscripts use brackets:
A final function argument can be written as a block after the call. Inside a short block, $0 means the first argument, $1 the second, and so on:
Here map calls the block once for each number. The compiler knows each $0 is an Int because the array contains integers. You can name the argument when that reads better: .map { number in number * 10 }.
Operators
The common operators are:
- arithmetic:
+,-,*,/ - comparison:
==,!=,<,<=,>,>= - Boolean:
!,&&,|| - bitwise:
&,|,^,~,<<,>> - propagation and force unwrap: postfix
?and! - conversion or existential packing:
as
The usual precedence rules apply, so multiplication happens before addition. Strings use + for concatenation:
Operators can also work with user-defined types. The matching protocol, such as Add or Comparable, defines what the operator does; Generics and Protocols explains how.
Names and visibility
Each source file is a module. Declarations stay inside that file unless you prefix them with pub:
pub func answer() -> Int { 42 }
Another file can then import answer. TalkTalk does not currently require type annotations on public declarations, though annotations often make a public API easier to understand. See Modules and Packages for imports and file layout.
A local name is available after its declaration and may reuse an earlier name. Function declarations are available throughout their enclosing block, including above the line where they are written.