I've been playing around with this via inheritance lately (I'm
aware of the sumtype package but specifically wanted to use
objects instead of structs), had some good results.
It involves a bit of boilerplate though. I'm essentially using
the visitor pattern + an anonymous class implementing a matcher
interface:
https://gist.github.com/dewf/dadc0f2775b40b65a3ebf7458d3c2c79
I would love to use templated return values from the match()
method, though. Does anybody know how to do that without the
compiler complaining ("'final' functions cannot be 'abstract'") ?
Specifically, is there any way to make:
abstract void match(Matcher m);
Become:
abstract T match(T)(Matcher!T m);
?