News for hackers. Stuff that matters.
Re:The visitor pattern is essentially the same thing as Chur... by tsimionescu on Thursday February 04, 2021 @06:15PM (#26030911)
Forgot one other important distinction: if we wanted to add a new expression subtype that can behave like one of the existing types for existing operations, that is not possible with the discriminated union variant. Say we wanted to add an SmallAddition, that stores its two numbers as bytes, but converts them to int when needed. We could add this without modifying any code in the visitors in the C# version (well, technically we may have a problem because we accessed the fields directly, but that's inconsequential). In the pseudo-Haskell version, we would need to modify all 6 evaluation functions just to handle the new Expression subtype.
Re:The visitor pattern is essentially the same thing as Chur... by dwohnitmok on Thursday February 04, 2021 @07:59PM (#26031788)
> In the pseudo-Haskell version, we would need to modify all 6 evaluation functions just to handle the new Expression subtype.

That's not true. I think you may be misunderstanding open unions (or polymorphic variants) here. Tags are types in and of themselves. That's why these are all `type alias`es. You could decide to modify the type alias if you'd like, but you don't have to. You can add the additional tag just to the call site.

Re:The visitor pattern is essentially the same thing as Chur... by tsimionescu on Friday February 05, 2021 @01:38AM (#26033734)
So how would a subtype of Addition look here? Who would tell stringEvaluator how to handle this SmallNumberAddition?
Re:The visitor pattern is essentially the same thing as Chur... by dwohnitmok on Saturday February 06, 2021 @05:02PM (#26050419)
There wouldn't be a subtype of Addition because Haskell doesn't have a notion of subtyping in the same way C# does. It would depend on the particulars of what the difference between SmallNumberAddition and Addition look like (in particular which parts are kept with the same functionality and which are different). The answer could be either a simple function or a typeclass depending on that.