gpt4 book ai didi

scala - 为什么我不能在Scala的this.apply(_)中省略 “apply”?

转载 作者:行者123 更新时间:2023-12-04 13:24:22 26 4
gpt4 key购买 nike

观察以下代码

trait Example {
type O
def apply(o: O)
def f(o: O) = this.apply(o)
}

在Scala中编译良好。我希望我可以像往常一样省略 apply,编写 def f(o: O) = this(o)。但是,这会导致令人兴奋的错误消息
type mismatch;  found   : o.type (with underlying type Example.this.O)
required: _31.O where val _31: Example
possible cause: missing arguments for method or constructor

谁能告诉我发生了什么事?

最佳答案

接受的答案不正确。您可以从编译的事实中推断出实际的问题是什么:

trait Example {
def apply(o: String): String = o
def f(o: String) = this(o)
}

当调用站点是辅助构造函数时,this(...)仅表示对构造函数的调用。就像您想象的那样,剩下的时间就是申请电话。

关于scala - 为什么我不能在Scala的this.apply(_)中省略 “apply”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8626735/

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