Slide 24 of 74

How echo shows a value

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}, {}]
}

echo writes one line to standard output, and panic and interpolation show a value the same way:

  • A Str on its own is the line, unquoted. Inside a vector, a record or a Result it is quoted, which is what tells ["1", "2"] from [1, 2] and from ["1 2"].
  • An Int and a Bool as written. A Float at its shortest: 3.0 shows as 3, 0.1 + 0.2 as 0.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 Result as Ok(…) or Error(…).
  • 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.