给出以下代码:
implicit class RichString(s: String) {
def !![T](createMessage: Long => T) = ()
}
case class SomeClass(i: Int, s: String, id: Long)
为什么会这样:
"some string" !! SomeClass(5, "test", _)
不编译,抛出以下错误:
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", _)
但所有这些都编译得很好:
val f = SomeClass(5, "test", _)
"some string" !! f
"some string" !! ( SomeClass(5, "test", _) )
"some string" !! { SomeClass(5, "test", _) }
PS:此"some string" !! SomeClass(5, "test", _: Long)
也会导致类型不匹配。
答案 0 :(得分:6)
正如消息所述,"some string" !! SomeClass(5, "test", _)
扩展为x => "some string" !! SomeClass(5, "test", x)
,不是 "some string" !! { x => SomeClass(5, "test", x) }
。因此!!
没有收到函数,它会收到SomeClass
,但不会编译。