Conventions
Variables and functions use
camelCase:userName,parseAge.Types and variants use
PascalCase:Person,FileError,NotFound.Parentheses are required around conditions (
if (x > 0),while (running),match (value)) and in function calls.Strings can use single (
') or double (") quotes, but the convention is to prefer single quotes. Backticks are for interpolation.Imports are written at the top of the file.
A variable is annotated only when its type is not evident from its value:
immut person = Person.create('Ana'), butimmut person: Person = { name: 'Ana', age: 30 }(an object literal needs the type).Variables are
immutunless they need to change.A blank line separates statements of different kinds: declarations, assignments, calls of different functions,
return, and blocks (if,for,while,match). Consecutive statements of the same kind, such as a few declarations or severalprintcalls, stay together. Functions, types and multi-line statements are always separated from their neighbors, and a comment on its own line goes with the statement below it:function factorial(x: int): int { if (x <= 1) { return 1 } return x * factorial(x - 1) } mut count = 0 immut name = 'Ana' count += 1 // Show the result. print(name) print(count)Special cases come first, with an early
return(orbreakandcontinuein a loop), so the main code is not nested insideifandelse:function describe(age: int): string { if (age < 0) { return 'invalid' } if (age < 18) { return 'minor' } return 'adult' }A comment after code on the same line is separated from it by exactly one space. Comments are not aligned into columns:
immut count = 0 // number of items immut total = 0.0 // sum of the pricesA file ends with exactly one line break: the last line is terminated, and no blank lines follow it.
Numbers
The rule: simple calculations use decimal; complex calculations, the ones that need speed, use float.
decimalis for simple calculations: money, measures, percentages, quantities, and data read from text or JSON. It is the type of a literal like1.5, and the one the standard library uses (time in seconds, the numbers of JSON,math.pi).floatis for complex calculations, where the speed of the processor matters more than base 10: graph algorithms, simulations, science, statistics on large data, graphics and geometry. It is created explicitly, withfloat(x)or a literal in hexadecimal (0x1.8p1), and its values are shown as they are stored.intis for counts, positions and amounts in a smallest unit (cents, milliseconds).