Pattern matching in an open world
- 7 April 2020
- journal article
- research article
- Published by Association for Computing Machinery (ACM) in ACM SIGPLAN Notices
- Vol. 53 (9), 134-146
- https://doi.org/10.1145/3393934.3278124
Abstract
Pattern matching is a pervasive and useful feature in functional programming. There have been many attempts to bring similar notions to Object-Oriented Programming (OOP) in the past. However, a key challenge in OOP is how pattern matching can coexist with the open nature of OOP data structures, while at the same time guaranteeing other desirable properties for pattern matching. This paper discusses several desirable properties for pattern matching in an OOP context and shows how existing approaches are lacking some of these properties. We argue that the traditional semantics of pattern matching, which is based on the order of patterns and adopted by many approaches, is in conflict with the openness of data structures. Therefore we suggest that a more restricted, top-level pattern matching model, where the order of patterns is irrelevant, is worthwhile considering in an OOP context. To compensate for the absence of ordered patterns we propose a complementary mechanism for case analysis with defaults, which can be used when nested and/or multiple case analysis is needed. To illustrate our points we develop Castor: a meta-programming library inScala that adopts both ideas. Castor generates code that uses type-safe extensible visitors, and largely removes boilerplate code typically associated with visitors. We illustrate the applicability of our approach with a case study modularizing the interpreters in the famous book ”Types and Programming Languages”.Keywords
This publication has 23 references indexed in Scilit:
- Modular reifiable matching: a list-of-functors approach to two-level typesPublished by Association for Computing Machinery (ACM) ,2015
- Feature-Oriented Programming with Object AlgebrasLecture Notes in Computer Science, 2013
- Extensibility for the MassesLecture Notes in Computer Science, 2012
- Finally tagless, partially evaluated: Tagless staged interpreters for simpler typed languagesJournal of Functional Programming, 2009
- Modular Visitor ComponentsPublished by Springer Science and Business Media LLC ,2009
- Data types à la carteJournal of Functional Programming, 2008
- Modular typechecking for hierarchically extensible datatypes and functionsACM Transactions on Programming Languages and Systems, 2004
- JMatch: Iterable Abstract Pattern Matching for JavaLecture Notes in Computer Science, 2002
- Template meta-programming for HaskellPublished by Association for Computing Machinery (ACM) ,2002
- MultiJavaACM SIGPLAN Notices, 2000