Implementing SAM traits which have type members using lambda syntax

A case like this will not compile

trait Test[A] {
  type T
  def apply(a: A): T
}

val test: Test[String] = s => s.length

type mismatch;
found : Int
required: Test[String]#T

changing the target type to specify the type member should not be needed, but does make things work in scala 3, however it does not help in scala 2

val test: Test[String] { type T = Int} = (s: String) => s.length

It would be nice if the compiler could infer T in the first example given that it is inside a SAM trait.

1 Like

Or heck, T could be inferred to be the LUB of all the methods that use it in an object.