3 ms·
> You've conveniently ignored the case of the non-drawing commands. Should they be Drawables with a Draw which does not draw anything? In particular, you can't
by pcwalton 12y ago
> You've conveniently ignored the case of the non-drawing commands. Should they be Drawables with a Draw which does not draw anything?
In particular, you can't implement Quit using a virtual method, unless you do something like have every function return a boolean to the caller indicating whether it wants to quit (or panic and recover, but that's too awkward for words).
You also can't have methods that set local variables in the caller, unless you do something like pass in their addresses to the function, which gets awkward quickly if you have a lot of local variables.
- azth 12y ago> You also can't have methods that set local variables in the caller... How would you do that using ADTs?
- pcwalton 12y agoYou don't use methods, but instead switch on the discriminant of the ADT and take different actions (such as updating local variables). This is pretty related to the so-called expression problem. OO-style methods are good at extensibility, but they're bad at adding extension points (method calls), since you have to update every one and calls are limited in expressive power compared to the body of a switch statement. Functional-style ADTs are bad at extensibility (adding a new type requires updating all pattern matches), but they're good at adding new extension points, since you don't have to touch the data types and pattern matching is powerful. Supporting both styles in a language gives programmers the freedom to choose the best one for the task at hand.
- azth 12y agoMakes sense. Thanks.
- Manishearth 12y agoWith ADTs you would not have individual methods, but instead a match block possibly referencing the local variables.
- Manishearth 12y agoI think the general pattern is to use a context object, but this gets complicated sometimes.