type circle = {radius: float, x: float, y: float}
let c = {radius: 1.1, x: 10., y: 20.2}
type shape =
| Square(float)
| Rectangle({width: float, height: float})
| Circle(circle)
let display_shape = s => {
open Js.Float
switch s {
| Circle({radius, x, y}) =>
Js.log(
`radius:${toString(radius)}, x:${toString(x)}, y:${toString(y)}`,
)
| Square(x) => Js.log(`Square of ${toString(x)}`)
| Rectangle({width, height}) =>
Js.log(`width:${toString(width)}, height:${toString(height)}`)
}
}
display_shape(Rectangle({width: 1.1, height: 2.2}))
display_shape(Circle(c))
/*
Limitation of inline record.
The following code will get compile time error -
'This form is not allowed as the type of the inlined record could escape.'
*/
let get_shape = s => {
switch s {
| Rectangle(r) => Some(r)
| _ => None
}
}
type point = Point(float, float)
let p = Point(1.1, 2.2)
let Point(x, y) = p
/* varint with tuple can be grabbed as a whole */
type point = Point((float, float))
let p = Point((1.1, 2.2))
let Point(p') = p
Js.log(p') // [ 1.1, 2.2]