johnlcox / motif

Scala-like pattern matching for Java 8
http://john.leacox.com/motif
Apache License 2.0
148 stars 4 forks source link

Come up with a solution to the overloaded method ambiguity problem #5

Closed johnlcox closed 9 years ago

johnlcox commented 9 years ago

In some situations the Java compiler is unable to determine the correct overload to call when using lambdas. See my Stackovervlow question for one example of the ambiguity problem. The ambiguity problem occurs for Function/Consumer overloads, but it also occurs for generic parameter/ArgumentMatcher overloads.

For example a lambda calling getPattern with overloads getPattern(Function<String, String> function) andgetPattern(Consumer<String> consumer) will fail to compile due to ambiguity. The same is true for the following overloads: getPattern(A, Function<A, String> function) and getPattern(ArgumentMatcher, Function<A, String> function).

My initial solution to the Function/Consumer problem is to follow the convention of naming all Function methods as case* and all Consumer methods as caze*. I'm not super happy about this, but I haven't come up with a better alternative so far.

I'm at more of a loss as to what to do for the A/ArgumentMatcher issue. I could drop the exact match APIs entirely and require matchers always, but this adds verbosity even when it's not needed.

If anyone has any thoughts on either of these ambiguity issues I am open to input.

johnlcox commented 9 years ago

Re-implemented Motif with a fluent interface that avoids this problem.