Skip to content
Eric Yang edited this page Jun 27, 2021 · 2 revisions

代数数据类型-ADT

ADT(Algebraic Data Type,代数数据类型)是复合类型,即由其他类型的组合形成的类型。

trait

type Show {
    show: This -> string
}

Sum type

type List [
    Nil,
    Cons(head: i32, tail: List)
]

Product type

type Person (
    age: i32,
    name: string
) impl Show {
    fun show(this: This) -> string {
        return "Hello Deeplang"
    }
}

// desugar
type Person [
    Person(age: i32, name: string)
] impl Show {
    fun show(this: This) -> string {
        return "Hello Deeplang"
    }
}
Clone this wiki locally