Custom Types
Array Types
You can use arrays as types.
let Person = [name: String, age: UInt]
let grow = (person: Person) -> person.age++
let lu = new Person("Lu", 28)
lu:grow()
Function Types
You can use functions as types.
let Even = [n] => n % 2 == 0
let half = [n: Even] => n / 2
half(4) //2
half(3) //Type Error
Checking
You can check a value's type with the is
or type
function.
3:is(Number) //true
3:is(UInt32) //true
3:type //UInt32
let Vector = [x: Int, y: Int]
let position = Vector(3, 2)
position:type //Person
position:is(Vector) //true
position:is(Int[2]) //true
Last updated
Was this helpful?