【发布时间】:2017-06-28 16:12:04
【问题描述】:
如果我有一个 Scala 特征,其中定义了两个函数,一个仅使用签名 def foo : Int => String 定义,另一个函数使用参数和返回类型 def bar(myInt : Int): String 声明,那么这些方法的行为会有所不同。
import org.scalamock.scalatest.MockFactory
import org.scalatest.{Matchers, WordSpec}
class DefTest {
trait DefTrait {
def foo : Int => String
def bar(myInt: Int) : String
}
class DefTest extends WordSpec with Matchers with MockFactory {
val defStub = stub[DefTrait]
defStub.bar _ when * returns "Works"
defStub.foo _ when * return "nope"
}
}
IntellJ 说有 Too many arguments for method when 和 expected: FunctionAdapter0[Boolean], actual: MatchAny。
SBT 说:
type mismatch;
[error] found : org.scalamock.matchers.MatchAny
[error] required: org.scalamock.function.FunctionAdapter0[Boolean]
[error] defStub.foo _ when * returns "nope"
[error] ^
这让我想知道:
- 这两种函数声明有什么区别?我认为它们是等价的,直到现在我似乎都可以互换使用它们。
- 是否可以将签名函数定义
foo: Int => String与defStub.foo _ when 42 return "yay"语法一起使用?
【问题讨论】: