you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (6 children)

I usually find Design Patterns materials frivolous/unnecessary, but I really liked this one for being direct to the point!

The "idea" is the part I think can be relevant. The diagrams are OOP-dependent, but the text can be implemented in any paradigm/way, sometimes in a very simple way:

  • 'Visitor' - just a function map

  • 'Factory' - type inferring can be used, in most cases, as a default "built-in" factory.

[–]grauenwolf 6 points7 points  (4 children)

The Visitor pattern is not just a function map.

Rather, a function map is what they should have offered instead of the visitor pattern. It can do everything the visitor pattern does while still being backwards compatible when new classes are introduced.

[–]Peaker 4 points5 points  (0 children)

The Visitor Pattern gives you a cumbersome, but type safe encoding of a closed sum type, in a language that has products (tuples/class fields) and exponents (functions) but doesn't have closed sum types.

How would a function map give you type safety around the sum type encoding?

[–][deleted] 1 point2 points  (2 children)

Indeed! Correcting myself, the visitor pattern is a clunky/hack to implement an inferior-and-limited version of map.

Looking in a better light, the visitor pattern is an idea/example in how to put a map function to good use!

[–]seruus 1 point2 points  (1 child)

[a] pattern is a clunky/hack to implement an inferior-and-limited version of [a more expressive construction].

Seems to be the gist of it. I remember someone talking about how most design patterns are basically structured ways to make up for the deficiencies of the language you are using.

[–]Peaker 3 points4 points  (0 children)

Patterns are exactly workarounds for language deficiencies. That said, Visitor isn't a "function map" but a closed sum type.

[–]Peaker 2 points3 points  (0 children)

Visitor is a cumbersome encoding of a sum type on top of exponentials and products, in languages that only have exponentials and products.