Is there a difference between (generics)

I have the following:

def someFunc(value: MyType = MyType()): MyType

Is any other anyway:

def someFunc[T >: MyType](value: T = MyType()): T
+4
source share
1 answer

Let's see what happens if I take Stringand try to transfer it to someFunc[T >: MyType]:

Definition:

trait MyType
def someFunc[T <: MyType](value: T = new MyType { }): T = value

Vocation:

someFunc("hello")

Productivity

hello

How is this possible? Since you indicated MyTypeas the lower bound for T, that is, we have MyType >: T <: Any. How does it work for String? If we consider the type Tas a AnyRefsupertype String, then the relationship type is MyType >: AnyRef <: Anychecked.

To answer your question: No, they are not identical because I cannot pass to the Stringfirst method that accepts MyType.

If you want to ask:

def someFunc[T <: MyType]

and

def someFunc(value: MyType)

, , T , , MyType, , MyType, , .

+2

Source: https://habr.com/ru/post/1688003/


All Articles