pub type User { User(name: String, age: Int) } pub type Shape { Circle(r: Float), Square(side: Float) } pub fn name :: u: User -> String { u.name } pub fn birthday :: u: User -> User { User(..u, age: u.age + 1) } pub fn make :: -> User { User(age: 3, name: "Ada") } pub fn bad_label :: -> User { User(name: "x", years: 3) //~ ERROR E0204 no parameter or field named `years` } pub fn missing_field :: -> User { User("x") //~ ERROR E0205 } pub fn no_such_field :: u: User -> Int { u.height //~ ERROR E0206 no field named `height` } pub fn several_ctors :: s: Shape -> Float { s.r //~ ERROR E0206 several constructors } pub fn area :: s: Shape -> Float { match s { Circle(r) -> 3.14 * r * r, Square(side: x) -> x * x } } pub fn pattern_arity :: u: User -> String { match u { User(n) -> n //~ ERROR E0205 } } pub fn pattern_rest :: u: User -> String { match u { User(name: n, ..) -> n } } // Callback parameters get their type from the expected function type. fn apply :: u: User f: fn(User) -> String -> String { f u } pub fn callback :: u: User -> String { apply u fn x { x.name } } // Record update copies the other fields by position, so the base must // have the constructor: a type with several can't be updated (issue #150). pub fn resize :: s: Shape -> Shape { Circle(..s, r: 1.0) //~ ERROR E0204 `Shape` has several constructors }