|
|
|
|
|
by Twisol
1961 days ago
|
|
> The things which are instances of Shape are functions that accept circles or rectangles, not actual circles or rectangles. The naming is confusing, but it's misled you in the opposite direction. Things that are instances of Shape are functions that accept handlers of circles or rectangles. The handlers, unfortunately, are named `circle` and `rectangle`. I would prefer `onCircle` and `onRectangle` in this context, because we're lacking the context of a true `match` expression to disambiguate the naming. type Shape = <T>(handlers: {
onCircle: (args: Circle) => T;
onRectangle: (args: Rectangle) => T;
}) => T;
> Also, you seem to have created functions and types with the same names (Circle and Rectangle) which seems dangerous.I think this is an idiom for defining a canonical constructor for a type. It's a little funky here because they're returning Shape, not Circle or Rectangle, and the latter are not subtypes of Shape. But it mostly tracks alongside the rest of the encoding. |
|
The ‘exampleCircle’ is not an instance of Circle, it’s an instance of Shape.
I like the ‘onCircle’ naming though.