Simão Martins
Simão Martins

Reputation: 1240

Type incorrectly inferred

Given the following code:

implicit class RichString(s: String) {
  def !![T](createMessage: Long => T) = ()
}
case class SomeClass(i: Int, s: String, id: Long)

Why is it that this:

"some string" !! SomeClass(5, "test", _)

Does not compile, throwing the following errors:

Missing parameter type for expanded function ((x$1) => "some string".$bang$bang(SomeClass(5, "test", x$1)))
 "some string" !! SomeClass(5, "test", _)
                                       ^

Type mismatch;
 found   : cmd1.SomeClass
 required: Long => cmd1.SomeClass "some string" !! SomeClass(5, "test", _)

But all of these compile just fine:

val f = SomeClass(5, "test", _)
"some string" !! f

"some string" !! ( SomeClass(5, "test", _) )

"some string" !! { SomeClass(5, "test", _) }

PS: this "some string" !! SomeClass(5, "test", _: Long) also throws the type mismatch.

Upvotes: 3

Views: 72

Answers (1)

Alexey Romanov
Alexey Romanov

Reputation: 170733

As the message says, "some string" !! SomeClass(5, "test", _) is expanded as x => "some string" !! SomeClass(5, "test", x), not "some string" !! { x => SomeClass(5, "test", x) }. So !! doesn't receive a function, it receives a SomeClass, which doesn't compile.

Upvotes: 6

Related Questions