Basic types
Type annotations
Anonymous type annotations
An anonymous type annotation that attaches to an expression instead of to a name.
expr::type specifies the type of an expression.
In a function call like to::int 1.5,
the type is the return type of the function.
You can also write :: type at the start of an indented block to type the block.
Expected types
When Keen type-checks an expression, it may have an expected type.
- A function body's expected type is the function's return type.
- A function argument's expected type is the function's declared argument type.
(Overloading complicates this; this will be explained in Overloading.) - A local's expected type is the declared type (if any).
- Any expression with an anonymous type annotation has that as an expected type.
- Certain expressions have an expected type;
for example, in a sequence expression, any subexpression but the last must be
void.
The below example uses () which can take on many types.
This will be fully explained in "new" functions.
Many expressions do not need an expected type.
For example, the call to foo does not need an expected type,
since there is only one foo and it returns a string.
Literal expressions
Literal expressions have favorite types that they will use automatically if there is no expected type.
A number literal prefers to be a nat, but a negative number will be an int
and a number with a decimal point (even if .0) will be a float.
A string literal prefers to be a string.
If there is an expected type, a literal expression will use that instead of its default type.
(More on expected types in Overloading.)
The possible number literal types are:
nat8,nat16,nat32,nat64(natis an alias fornat64)int8,int16,int32,int64(intis an alias forint64)float32,float64(floatis an alias forfloat64)json
The possible string literal types are:
For char8 or char32, the string literal must contain a single character.
String literals can even be a user-defined type; see String literals.