Home > Software design >  what does more than one Parameterized Types mean?
what does more than one Parameterized Types mean?

Time:09-01

i'm learning some scala example from somewhere, it looks like as beblow:

class className[T <: classTypeA : classTypeB](some args)....

or

class className[T<:classTypeA with trait1](some args)...

some tutorial explain that T<:classTypeA means T must be the subclass of classTypeA. i don't understand why there are two types after "<:", and what "with trait1" means;

CodePudding user response:

class className[T <: classTypeA : classTypeB](some args)

In this declaration classTypeB is a type class and this is equivalent to

class className[T <: classTypeA](implicit ev: classTypeB[T])(some args)

There must be a suitable instance of classTypeB[T] in scope for this to compile. This allows fine-grain control of which types are accepted as parameters to the class.


class className[T <: classTypeA with trait1](some args)

This declaration says that T must be a subclass of the type classTypeA with trait1.

  • Related