Re: Generics headache
On Mon, 28 Jul 2008, Lew wrote:
Tom Anderson wrote:
What you did say, upthread, was:
Notice that the abstract 'parse()' method, the sole purpose of the
'Parser' interface, its entire raison d'etre, does not use type 'T'.
That means that the type parameter is not necessary.
And to me, that means exactly the same.
That particular post related to the simplified example that didn't
even have doAfterEachMatch in it. Apples and oranges.
Ah, okay, then everything is in order.
Anyway, since i have clearly failed at reading comprehension, i don't
suppose there's any chance of you being so generous as to explain what you
*did* mean, is there?
Oh, nice sarcasm there, buddy. You know perfectly well the OP's example
made no mention of this other method, then later he did. So some
comments pertained to one example, some to another. I don't suppose
*you'd* be so generous as to cut a guy some slack for having been
confused by the OP's change of context, would you?
Oh, well alright then.
Object for a value. Would you factor out a subclass
PostMatchActionParser<ParserMatchingType>, and push the method down to
that?
Would you?
If i [sic] was dead set on eliminating the type variable from Parser,
then yes.
I was asking for your insight in how to do the refactoring, if there's
any chance that you'd be so generous as to share the wisdom.
But of course!
Currently, we have something like (but only 'like'):
abstract class Parser<T> {
public abstract AST parse(Scanner scanner) ;
public abstract void doAfterEachMatch(T value) ;
}
abstract class InterceptableParser<T> extends Parser<T> {
// there isn't actually any foo, but YKWIM
// the actual code is a bit too complicated to reproduce
public void foo(T value) {
this.doAfterEachMatch(value) ;
}
}
I would refactor to:
abstract class Parser {
public abstract AST parse(Scanner scanner) ;
}
abstract class ActionAfterMatchParser<T> extends Parser {
public abstract void doAfterEachMatch(T value) ;
}
abstract class InterceptableParser<T> implements ActionAfterMatchParser<T> {
public void foo(T value) {
this.doAfterEachMatch(value) ;
}
}
I'd also change the purely abstract classes to interfaces.
It is a well-trained gut.
With an acid-reflux condition?
As it happens, yes. It's hereditary.
tom
--
an optical recording release. copyright digitally mastered. .,