So here is some real code. I prefer looking at real code to toy examples, where possible.
implied ValueAndThenPosition[Buffer, A] for ParseOneThenOther[Value[Buffer, A], Position[Buffer], Value[Buffer, A]] =
(lhs, rhs) => (buff, pos) => lhs(buff, pos)(new {
override def matched(lEnd: NonNegativeInt, value: A) = rhs(buff, lEnd)(new {
override def matched(rEnd: NonNegativeInt) = ValueResult.wasMatch(rEnd, value)
override def mismatched = ValueResult.wasMismatch
})
override def mismatched = ValueResult.wasMismatch
})
It’s part of my toy parser combinator library. There are nested call-back instances that handle the parser success and failure conditions. It is, I freely admit, a horrible heap of spaghetti to read. But the point I want to make is that the new
keyword is shot through, and it really doesn’t help any, and ideally once the compiler has got at this, there should be no or minimal allocations.
implied ValueAndThenPosition[Buffer, A] for ParseOneThenOther[Value[Buffer, A], Position[Buffer], Value[Buffer, A]] =
(lhs, rhs) => (buff, pos) => lhs(buff, pos){
def matched(lEnd: NonNegativeInt, value: A) = rhs(buff, lEnd){
def matched(rEnd: NonNegativeInt) = ValueResult.wasMatch(rEnd, value)
def mismatched = ValueResult.wasMismatch
}
def mismatched = ValueResult.wasMismatch
}
This is the same thing with new
removed, and with the “block treated as implementation” heuristic. I’ve also stripped out override
.
If we were also allowed to in-place (un-)curry method implementations, I could have written:
implied ValueAndThenPosition[Buffer, A] for ParseOneThenOther[Value[Buffer, A], Position[Buffer], Value[Buffer, A]] =
(lhs, rhs) => (buff, pos) => lhs(buff, pos){
def matched = (lEnd, value) => rhs(buff, lEnd){
def matched = (rEnd) => ValueResult.wasMatch(rEnd, value)
def mismatched = ValueResult.wasMismatch
}
def mismatched = ValueResult.wasMismatch
}
While this is still not ideal, it is, I’d contend, far more readable than the original. We’ve got rid of nearly all the explicit type annotations and (which is a big win for dyslexics like me) reduced the nested brackets and braces substantially.