2017-08-24 32 views
2

以下特点区别声明2个功能:f1和f2斯卡拉,协变和边界:在功能

trait Test[+T] { 
    def f1[U >: T](a: U): Boolean 
    def f2[U](a: U): Boolean 
    } 

他们是一样的吗?如果不是,有什么区别?

+0

好了,你可以通过任何类型的对象,以这两种功能 - 见[这个问题](https://stackoverflow.com/q/37334674/6231376)的说明。 – TeWu

+0

谢谢@TeWu – den123

回答

1

我认为就行为而言它们是相同的。两种功能都需要针对任何类型实施。但是类型与向编译器提供更多信息以帮助您防止错误等一样多,而且我可以想到一些可能比另一个更可取的陌生角落案例。例如:

class Foo 
class Bar extends Foo 

object BarChecker extends Test[Foo] { 
    def f1[U >: Foo](u: U): Boolean = { 
    if (u.isInstanceOf[Bar]) { 
     true 
    } else { 
     throw new RuntimeException("fail!") 
    } 
    } 
    def f2[U](u: U): Boolean = { 
    if (u.isInstanceOf[Bar]) { 
     true 
    } else { 
     throw new RuntimeException("fail!") 
    } 
    } 
} 

val x = BarChecker.f1[Foo](_) // can't do BarChecker.f1[Bar](_) 
x(new Bar) // true 
x(new Foo) // runtime exception 
val y = BarChecker.f2[Bar](_) 
y(new Bar) // true 
y(new Foo) // compile error 

当然你可以用解决这个问题:

val x: Bar => Boolean = BarChecker.f1[Foo](_) 

但我的观点是不同类型的签名可以有哪些错误,使用的时候有可能或有可能取得不同的效果。

+1

T.f1 [Foo](_)'中的T是什么? – TeWu

+0

哎呀,BarChecker最初名为T,但我(不正确/不完整)将其更改为更具描述性。将编辑。 –

1

在执行方面,他们是一样的。两者都带有U型参数,并返回布尔类型的结果。但是,就类型参数而言,它们不是。方法f1有一个lower bound类型参数但是方法f2不。

那么,这是什么意思? 在方法f2的情况下,您可以提供任何类型参数。在方法f1的情况下,您只能提供类型参数,其类型为TsuperTypeT。例如:

class Foo1(name:String) 
class Foo2(name:String) extends Foo1(name) 
class Foo3(name:String) extends Foo2(name) 

class Test[+T] { 
    def f1[U >: T](a: U): Boolean = true 
    def f2[U](a: U): Boolean = true 
    } 

val obj: Test[Foo2] = new Test[Foo2] 

val foo1: Foo1 = new Foo1("1") 
val foo2: Foo2 = new Foo2("2") 
val foo3: Foo3 = new Foo3("3") 

//F2 execute on any type you provide. 
testInstance.f2[Foo1](foo1) 
testInstance.f2[Foo2](foo2) 
testInstance.f2[Foo3](foo3) 

testInstance.f1[Foo2](foo2)  //Foo2 is equal to type T. 
testInstance.f1[Foo1](foo1)  //Foo1 is supertype of T - Foo2. 
testInstance.f1[Foo3](foo3)  //Does not compile, because Foo3 is not superType of T - Foo2. 

事实上,在Scala中,在Co-variance注释[+ T]的情况下,必须定义一个下界。以下将失败:

class Test[+T] { 
    def f1(a: T): Boolean = true  //This will fail. 
    def f2[U](a: U): Boolean = true 
    }