π₯Tagged Unions
Syntax
enum <TaggedUnionName> {
case <case name>(<FieldName> : <FieldType>, ...) // case syntax
}
Example
package main
import "fmt" as fmt
import "strconv" as strconv
enum Shapes {
Circle(diameter: Int, color: String)
Square(width: Int, height: Int, color: String)
Triangle(base: Int, width: Int, height: Int, color: String)
}
fun main(): Unit {
fmt.println("I only like red triangles. Let's see...")
let myShape = Triangle(base: 10, width : 5, height : 20, color : "red")
match myShape {
case Triangle(base: let b, height: let height, color : "red") : {
fmt.println("It's a red triangle with a height of " + strconv.itoa(height) + " and base length of " + strconv.itoa(b) + "!")
}
case Square(width : let width, color : "red") : {
fmt.println("It's a red square with a width of " + strconv.itoa(width))
}
case Circle(diameter: let diameter, color : "red") : {
fmt.println("It's a red circle with a diameter of " + strconv.itoa(diameter))
}
}
}
Last updated