Data types and enums
data types hold a few values together; enums are a value that is one of a fixed set of cases, which can each carry their own data. Together they describe most of a program's information.
#Data types
A data type is declared with its fields, and gets equality, printing and copying for free:
data User(name: String, age: Int)
let ada = User("Ada", 36)
println(ada)
println(ada.name)
println(ada == User("Ada", 36))User(name=Ada, age=36) Ada true
Data values never change. copy makes a new one with some fields changed, by name:
data User(name: String, age: Int)
let ada = User("Ada", 36)
let older = ada.copy(age = 37)
println(older)
println(ada)User(name=Ada, age=37) User(name=Ada, age=36)
#Named arguments and defaults
Fields can have default values, and values can be given by name:
data Config(host: String = "localhost", port: Int = 8080, debug: Bool = false)
println(Config())
println(Config(port = 3000))
println(Config("example.com", debug = true))Config(host=localhost, port=8080, debug=false) Config(host=localhost, port=3000, debug=false) Config(host=example.com, port=8080, debug=true)
#Methods for data types
Data types have no body. Functions for them are extension functions:
data Point(x: Float, y: Float)
fn Point.distanceTo(other: Point): Float = sqrt(pow(x - other.x, 2) + pow(y - other.y, 2))
fn Point.plus(other: Point): Point = Point(x + other.x, y + other.y)
let a = Point(0.0, 0.0)
let b = Point(3.0, 4.0)
println(a.distanceTo(b))
println(a.plus(b).plus(b))5.0 Point(x=6.0, y=8.0)
#Generic data types
data Pair<A, B>(first: A, second: B)
let p = Pair("answer", 42)
println(p.second + 1)43
#Enums
A simple enum lists its cases. Each case is used by its name:
enum Color { Red, Green, Blue }
let favorite = Green
println(favorite)
println(favorite == Color.Green)
println(Color.entries)Green true [Red, Green, Blue]
Color.Green and Green are the same; the long form is needed only when two enums have a case with the same name.
#Enums with data
Each case of an enum can carry its own fields. A value of the enum is exactly one of the cases:
enum Shape {
Circle(radius: Float)
Rect(width: Float, height: Float)
Empty
}
let shapes = [Circle(1.5), Rect(2.0, 3.0), Empty]
println(shapes)[Circle(radius=1.5), Rect(width=2.0, height=3.0), Empty]
The fields are read with match, which makes you handle every case:
enum Shape {
Circle(radius: Float)
Rect(width: Float, height: Float)
Empty
}
fn area(shape: Shape): Float = match shape {
Circle(r) -> PI * r * r
Rect(w, h) -> w * h
Empty -> 0.0
}
println(area(Rect(2.0, 3.0)))
println(area(Empty))6.0 0.0
This is how Zyn describes things that can be one of several kinds: messages, commands, the nodes of a syntax tree, the states of a game. Add a case, and the type checker shows every match that must handle it.
#Generic enums
Enums can have type parameters. A case that doesn't use one fits any of them:
enum Tree<T> {
Leaf(value: T)
Node(left: Tree<T>, right: Tree<T>)
}
fn sum(tree: Tree<Int>): Int = match tree {
Leaf(v) -> v
Node(l, r) -> sum(l) + sum(r)
}
println(sum(Node(Leaf(1), Node(Leaf(2), Leaf(3)))))6
The built-in Result is an enum like this, written in Zyn: enum Result<T, E> { Ok(value: T), Err(error: E) }.
#Methods for enums
Like data types, enums get functions through extension functions:
enum Direction { North, East, South, West }
fn Direction.opposite(): Direction = match this {
North -> South
East -> West
South -> North
West -> East
}
println(North.opposite())South
#Where they go
data and enum types are declared at the top level of a file, and can be used before the line that declares them. They can be imported from other files by name.