You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We have Record that can be used to describe product data types via intersection types.
I am wondering, can we generalize it to support sum types via union types? Like iron use intersection types for AND constraints, union types for OR constraint.
For example:
typecircle="radius"~Doubletypesquare="length"~Doubletyperectangle="width"~Double&"height"~Doubletypetriangle="_1"~Double&"_2"~Double&"_3"~Doubletypeshape= ("typ"~"circle"& circle) | ("typ"~"square"& square) |
("typ"~"rectangle"& rectangle) | ("typ"~"triangle"& triangle)
defarea(shape: ADT[shape]):Double=
shape.typ:"circle"|"square"|"rectangle"|"triangle"// type of common fields derives via union type// shape.radius is not compiling
shape matchcaseLike[circle](c) => c.radius * c.radius * math.PicaseLike[square](s) => s.length * s.length
caseLike[rectangle](r) => r.width * r.height
caseLike[triangle](t) =>valp= (t._1 + t._2 + t._3) /2
math.sqrt((p - t._1) * (p - t._2) * (p - t._3))
// exhaustiveness can potentially be checked by macro
println(area("typ"~"circle"&"radius"~1.0))
// area("radius" ~ 1.0) // is not compiling
We have
Record
that can be used to describe product data types via intersection types.I am wondering, can we generalize it to support sum types via union types? Like iron use intersection types for
AND
constraints, union types forOR
constraint.For example:
I have roughly implemented macro and
ADT
class to make this example compiling:https://github.com/road21/extensible-adts
The text was updated successfully, but these errors were encountered: