gpt4 book ai didi

scala - 类型推断错误

转载 作者:行者123 更新时间:2023-12-01 09:07:11 26 4
gpt4 key购买 nike

给定以下代码:

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:这个“一些字符串”!! SomeClass(5, "test", _: Long) 也会抛出类型不匹配。

最佳答案

正如消息所说,“一些字符串”!! SomeClass(5, "test", _) 扩展为 x => "some string"!! SomeClass(5, "test", x), 不是 "some string"!! { x => SomeClass(5, "test", x) }.所以 !! 没有收到一个函数,它收到一个 SomeClass,它不会编译。

关于scala - 类型推断错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36572949/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com