As rolfl said, this is a strange idea. Imagine you want to compute x y for two int s. Sometimes it is undefined and you would use Maybe<Integer> ? And then another implementation, for example. nCr (x, y)?
Sounds wrong, doesn't it? The problem is that you connect the origin of the thing (intersection, power, choice) with the thing itself. But the intersection of two Shape again no more than a Shape (or nothing at all that can be well represented through Optional . Or even better with null , just call me at the old school).
The OO approximation does not make sense here, since there is no new type of object. 2 2 is the same as nCr (4, 1), and both are exactly the same as 4.
Another thing is that you must call the ShapesIntersection constructor. This is actually a static call, so you can write a static helper method instead.
Extending Shape some IntersectableShape may make sense. There are times when some operations are common enough for such a thing, see, for example, FluentIterable , but I doubt that you will make so many intersections.
maaartinus
source share