Parametrized method and cyclic inference

Dan Smith daniel.smith at
Sat Nov 3 10:40:22 PDT 2012

On Nov 2, 2012, at 6:27 PM, Remi Forax <forax at> wrote:

>> Thanks!  Feedback from real code is very useful.
>> I'm confused about your emphasis on the implicit parameter type -- if your lambdas look like '(Integer x) -> 3', don't you still get an error?
> No. Only, x -> 3 raises a cyclic inference error.

I tried compiling this:

  Set<Mapper<Integer, Object>> set = Collections.singleton((Integer x) -> 3);

And got an error: error: method singleton in class Collections cannot be applied to given types;
  Set<Mapper<Integer, Object>> set = Collections.singleton((Integer x) -> 3);
  required: T
  found: (Integer x)->3
  reason: cyclic inference - cannot infer target type for given lambda/method reference expression
  where T is a type-variable:
    T extends Object declared in method <T>singleton(T)

This matches my expectation -- the compiler has to decide what the type of the lambda expression is before it looks at the target; there's no way to know that 'Mapper' is the intended type.

So, are the use cases you're actually concerned about different than 'Collections.singleton'?  Or are you mistaken about adding an explicit parameter type fixing the problem?  Or maybe we're working with different compilers?

>> Yes, #1 is hopeless -- there is no type anywhere to tell us what the lambdas represent.
> I agree.
> But what about, m((String x) -> 3, x -> 4),
> i.e when on argument provide full types ?

What is the type of the lambda expression?  We'd have to pull a functional interface out of the air.

>> How important are these use cases to you?
> 17 out 23 usages of lambdas requires to declare parameters, it's perhaps just this codebase, but the question that comes in my mind is:
> Is it a good idea to provide a special syntax (no need to declare parameter type in lambda) if this syntax can rarely be used ?

Agreed; it is our intent that inference should be powerful enough to make explicit parameter types generally unnecessary.  Any of your use cases that fail with implicit parameters but succeed with explicit parameters are examples of where the inference implementation likely needs to be improved.

But I don't think 'singleton' falls into this category -- hence my question of whether use cases like 'singleton' are important.  (More generally, I'm talking about methods that don't mention functional interfaces in their declarations.)


More information about the lambda-spec-experts mailing list