Trait需要`call-by-value`,但是实现尝试`call-by-name`

时间:2016-04-02 15:33:26

标签: scala

假设:

scala> trait F {
     |   def foo(x: Int): Int
     | }
defined trait F

我尝试创建F的实现,但我创建了foo的参数call-by-name而不是call-by-value

scala> class FImpl extends F {
     |   override def foo(x: => Int): Int = 55
     | }
<console>:12: error: class FImpl needs to be abstract, since method foo in trait F of type (x: Int)Int is not defined
(Note that Int does not match => Int)
       class FImpl extends F {
             ^
<console>:13: error: method foo overrides nothing.
Note: the super classes of class FImpl contain the following, non final members named foo:
def foo(x: Int): Int
         override def foo(x: => Int): Int = 55
                      ^

编译器解释了我尝试失败的原因。

根据我的示例,是否可以定义F#foo函数,该函数采用call-by-namecall-by-value参数?

0 个答案:

没有答案