The Visitor of the Progenitor
Traverse varied magical forms natively with pattern matching.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Visitor
ARCANUM SCHOOL: Divination // Structural Insight
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CODEX INCANTATION //
datatype mystical_entity =
Elemental of string * int
| Spirit of string
| Golem of int
fun banish (Elemental (elem, power)) =
print ("Dispelling the " ^ elem ^ " elemental of power " ^ Int.toString power ^ ".\n")
| banish (Spirit name) =
print ("Exorcising the spirit known as " ^ name ^ ".\n")
| banish (Golem weight) =
print ("Crushing the " ^ Int.toString weight ^ " ton golem into dust.\n")
val entities = [Elemental ("Fire", 9000), Spirit "Banshee", Golem 5]
val _ = app banish entities
In object-oriented tongues, adding a new operation to a class hierarchy without modifying the classes requires the cumbersome Visitor pattern. In the realm of the ML Progenitor, this problem evaporates. Using algebraic datatypes and pattern matching, you can easily define external functions (like banish) that match over the types and operate upon them. The Visitor is native; the Visitor is SML.
Runic Tags:
#pattern matching
#datatypes
#traversal