Types, patterns and valuesSlide 26 of 86
26echoTypes, patterns and values
How echo shows a value
Grammar: echo-stmt
echo-stmtSpecification §2.5
echo-stmt = "echo" expression ;echo writes one line to standard output, and panic and interpolation show a value the same way:
- A
Stron its own is the line, unquoted. Inside a vector, a record or aResultit is quoted, which is what tells["1", "2"]from[1, 2]and from["1 2"]. - An
Intand aBoolas written. AFloatat its shortest:3.0shows as3,0.1 + 0.2as0.30000000000000004, and a large one with an exponent,1.23456789123e+08. - An atom by its name — except inside a string, where it is its number.
- A vector as its elements in brackets, and a
ResultasOk(…)orError(…). - A declared value as its fields in braces, and nothing else:
User(1, "ada")shows as{1, "ada"},Shape.Circle(3)as{3}, and a variant carrying nothing as{}. The type and the variant are not shown, so a program that needs a readable name writes one.
The one value none of them shows is a Secret, on the next slide.
type User {
id: Int
name: Str
}
type Shape {
Circle { radius: Int }
Point
}
func half(n: Int): Result<Int, Str> {
if n % 2 == 0 {
return Result.Ok(n / 2)
}
return Result.Error("odd")
}
proc main(): void {
echo "plain" // plain
echo ["plain", "1"] // ["plain", "1"]
echo 3.0 // 3
echo 0.1 + 0.2 // 0.30000000000000004
echo #Ready // Ready
echo half(4) // Ok(2)
echo half(3) // Error("odd")
echo User(1, "ada") // {1, "ada"}
echo [Shape.Circle(3), Shape.Point()] // [{3}, {}]
}You can edit this program and run it here.Tab indents, Escape leaves the program, and Ctrl+Enter runs it.
Output
Run a program to see what it prints here.