The argument types of an anonymous function must be fully known. (SLS 8.5)

function-literal, lambda, pattern-matching, scala

Solution

Here's the SLS quote, for the rest of us:

The expected type of such an expression must in part be defined. It must be either `scala.Functionk[S1, . . . , Sk, R]` for some k > 0, or `scala.PartialFunction[S1, R]`, where the argument type(s) S1, . . . , Sk must be fully determined, but the result type R may be undetermined.

Otherwise, you answered your question.

Problem

I have a function literal ``` {case QualifiedType(preds, ty) => t.ty = ty ; Some((emptyEqualityConstraintSet,preds)) } ``` Which results in an error message ``` missing parameter type for expanded function The argument types of an anonymous function must be fully known. (SLS 8.5) Expected type was: ? => Option[(Typer.this.EqualityConstraintSet, Typer.this.TypeRelationSet)] ``` I looked in SLS 8.5, but didn't find an explanation. If I expand the function myself to ``` {(qt : QualifiedType) => qt match {case QualifiedType(preds, ty) => t.ty = ty ; Some((emptyEqualityConstraintSet,preds)) }} ``` the error goes away. (a) Why is this an error? (b) What can I do to fix it? I tried the obvious fix, which was to add `: QualifiedType` between the pattern and the =>, but this is a syntax error. One thing I noticed is that the context makes a difference. If I use the function literal as an argument to a function declared as expecting a `QualifiedType => B`, there is no error. But if I use it as an argument to a function expecting an `A => B`, there is an error. I expect that what is going on here is that, as the pattern could conceivably be applied to an object whose type is a supertype of QualifiedType, the compiler is not willing to assign the obvious type without assurance that the function won't be applied to anything that isn't a QualifiedType. Really what I'd like is to be able to write `{QualifiedType( preds, ty) => ...}` and have it mean the same thing as Haskell's `\QualifiedType(preds,ty) -> ...`.

Original source