看着scala.Option[T]
來源我發現了下面的隱式參數聲明implicit ev: Null <:< A1
。嘗試它自己A <:<SomeType參數聲明
class Test[T](val i: Int){
def test(p: T <:< Option[Int]) = 1
}
我發現p
看起來像一個Function1
。它有apply
,andThen
等方法。會有什麼區別,如果我們寫:
class TestMatch[T](val i: Int){
def test(p: T <:< Option[Int]) = //..
def test2(p: T => Option[Int]) = //...
}
是否存在的test
和test2
singatures之間的一些主要區別?這個例子beahves是這樣的:
tm.test2(x => { //fine
println(x)
Some(x)
})
tm.test(x => { //Compile error
println(x)
Some(x)
})
我也試過這樣:
tm.test(x <:< { //Compile error
println(x)
Some(x)
})
,但它也不能工作。如何使用test
?
但是我該如何使用'test'?有沒有辦法調用它? – stella
但是'<:<'是一個函數。它在'Predef'中被定義爲''封閉的抽象類<:<[ - From,+ To] extends Serializable' – Kolmar
@Kolmar仍然不會使'p'成爲Function1。它只意味着約束使用中綴表示法,它實際上定義爲'<:<[T,Option [Int]]' –