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

I fought with the following compilation error a long time, and the error was completely different than I thought. I wonder should I have known from the error messages?

missing parameter type for expanded function
The argument types of an anonymous function must be fully known. (SLS 8.5)
Expected type was: ((List[(K, Iterable[V1])], List[(K, Iterable[V2])]), List[(K, (V1, V2))]) 

Here is the code

object Extraction {
  
  def join[K : Ordering,V1,V2](stations:Iterable[(K,V1)],temperatures:Iterable[(K,V2)]):Iterable[(K,(V1,V2))] = {

    def client(pair: (List[(K, Iterable[V1])], List[(K, Iterable[V2])])): ((List[(K, Iterable[V1])], List[(K, Iterable[V2])]), List[(K, (V1, V2))]) = {
      case (g1: List[(K, Iterable[V1])], g2: List[(K, Iterable[V2])]) =>
        if (g1 == ???)
          ???
        else if (g2 == ???)
          (???, Nil)
        else {
          ???
        }
    }
    ???
  }
  
  def main(argv:Array[String]):Unit = {
    ???
  }
}

The problem was that the function client has a parameter named pair which was never used. In fact I intended to include pair match { ...} surrounding the case (g1,g2) => ... But the pair match was missing.

In this case the error message isn’t really helpful, because the error described in it is not really the one you made, but is a “secondary error” caused by it.

By leaving out the match, this part of your code becomes an anonymous (partial) function (see the part of the spec referenced in the error):

{
      case (g1: List[(K, Iterable[V1])], g2: List[(K, Iterable[V2])]) =>
        if (g1 == ???)
          ???
        else if (g2 == ???)
          (???, Nil)
        else {
          ???
        }
    }

The type for it’s argument isn’t defined, as the expression at this point is expected to be of the return type of your client method, which isn’t a function type. So now you have an anonymous function with unknown parameter types, and that causes the error you see.

So it is difficult to find the error from the compiler message in such a case, especially as one doesn’t expect to have written an anonymous method at all. Maybe a hint could be added to that message, when the anonymous function occurs in a place where a known type, that isn’t a function type, is expected.

Would it be possible to print some of the text of the anonymous function? The the user would think: “why does the compiler think case (g1…) is an anonymous function? Ohhhhh I see…”

Potentially another reason I didn’t see the missing match was because the parameter list of the function on the previous line is very long. I experimented with ways of making that signature shorter by using local type definitions. I was convinced for a long time that those local type definitions were the cause of the mysterious error. Something like, can used type parameter variables inside local functions used in pattern matching because of type erasure. But that was a long-sought red herring.