Lesson 2: Functions as contracts
Write functions and let types take care of what goes in and what comes out.
Goal
In one sentence: declare functions with readable signatures and explicit types.
Code
func greet(name: str): str {
return "Hello, " + name
}
func double(value: int): int {
return value * 2
}
Types come after the parameters and after the : of the return. Explicit signatures make a function boundary and its contract visible to both the reader and the compiler.
Try it
Call double with a number, then try passing a str. The compiler stops the error right away, before anything even runs.
Tip
Type checking is the cheapest form of testing: the compiler verifies contracts for you on every build, without writing a single assert.
Next, we ask who may modify each value and for how long.