Re:The visitor pattern is essentially the same thing as Chur...
by _old_dude_ on Thursday February 04, 2021 @07:00AM
(#26024626)
> The downside is that Java doesn't let you implement generic methods with lambdas ...
yes, that exactly why i've chosen to make the type parametric and not the method apply, but you're right that the classical visitor pattern uses a polymorphic method (accept) not a polymorphic class.
Re:The visitor pattern is essentially the same thing as Chur...
by Twisol on Thursday February 04, 2021 @07:02AM
(#26024638)
Sure. Then, if you're set on using lambdas, I'd recommend this, to at least take away the ability to choose <T> from the caller.
static Shape<?> Circle(int x, int y, int r) {
return (circle, rectangle) -> circle.apply(x, y, r);
}
static Shape<?> Rectangle(int x, int y, int w, int h) {
return (circle, rectangle) -> rectangle.apply(x, y, w, h);
}
The downside is that Java doesn't let you implement generic methods with lambdas, which is silly, so you have to use an anonymous subclass. But at least the call-side uses stay the same.
(And then, combining the `_Circle` and `_Rectangle` interfaces gets you to a more canonical visitor pattern, at the expense of lambdas at the call-site.)