如果我有一个 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"
语法的签名函数定义?