Este artículo de F# describe la diferencia entre la definición de tipos usando clases vs usando TADS: en las clases es fácil añadir nuevos tipos de valores (creando subclases) y difícil añadir nuevas operaciones (métodos); en los TADs, es al contrario.
F# Overview (II.) - Functional Programming | Blog | TomasP.Net
F# Overview (II.) - Functional Programming | Blog | TomasP.Net
Discriminated unions form a perfect complement to the typical object-oriented inheritance structure. In an OO hierarchy the base class declares all methods that are overridden in derived classes, meaning that it is easy to add new type of value (by adding a new inherited class), but adding a new operation requires adding method to all the classes.
On the other side, a discriminated union defines all types of values in advance, which means that adding a new function to work with the type is easy, but adding a new type of value (new constructor to the discriminated union) requires modification of all existing functions. This suggests that discriminated unions are usually a better way for implementing a Visitor design pattern in F#.
No hay comentarios:
Publicar un comentario