Open mlhaufe opened 1 year ago
How are anonymous declarations extended?
Another interesting use case:
const Monoid = data((Monoid, T) => ({
Unit: T,
Merge: { left: T, right: T }
}))
const List = data((List, T) => ({
[extend]: Monoid(T),
Nil: {
[extend]: Monoid(T).Unit
},
Cons: {
[extend]: Monoid(T).Merge,
head: T, tail: List(T)
}
}))
https://wiki.haskell.org/Monoid https://wiki.haskell.org/Foldable_and_Traversable
In Scala it looks like this:
In this example, Cartesian3 and Polar3 are defined as case classes that extend the Cartesian2 and Polar2 case classes, respectively. They also mix in the Point3D trait, which extends the Point2D trait. This allows us to use Cartesian3 and Polar3 values wherever Point3D or Point2D values are expected.
In Brevity this could look like: