|
Re: pattern-matching is a bit low-level
|
Posted: Jul 3, 2006 4:28 AM
|
|
James Watson wrote:
> What I was thinking was more of a system where the cases > are actually Objects and can be mixed-in dynamically but > with the power of the pattern matching that you already > provide. The main problem with this would be that I don't > know how you could control the order of evaluation (given > you don't know the complete set of rules) and that often > (as in the given example) the rules and types are very > much coupled together. > > I guess I'm not really complaining about the > pattern-matching but more wishing for something. > > PS. If what i am talking about here is already possible in > Scala, I'd love to be enlightened.
If I understand you correctly, you want to be able to combine independent extensions of data and operations. You can do this with mixin composition. Let's assume we have a base system of algebraic terms, with a simplifier.
class Simplifier { def apply(term: Term): Term = term match { ... } }
Now, let's say I extend the base system with new case classes and a new simplifier:
class MySimplifier extends Simplifier { def apply(term: Term): Term = term match { case ... => ... case ... => ... case _ => super.apply(term) } }
The new simplifier handles all the cases I care about in my extension. If none of the new cases match, it passes control to super. As an independent extension, you add other case classes and another simplifier:
class YourSimplifier extends Simplifier { def apply(term: Term): Term = term match { case ... => ... case ... => ... case _ => super.apply(term) } }
Now, if I want to combine my extensions with your extensions, I can combine the two simplifiers as follows:
class CombinedSimplifier extends MySimplifier with YourSimplifier
This will thread the super calls through all simplifiers. I.e. in
(new CombinedSimplifier).apply(aterm)
first, your simplification rules will be tried; if none of them matches, my extended simplification rules will be tried, and if none of these matches, the base simplification rules will be tried.
This behavior also resembles a lot Kay's chainlets, I think.
|
|