Reputation: 16723
Two questions
Questions 1 What is the use of a class which takes type parameter
I could understand the usefulness of
trait SomeTrait[T]
I am struggling to comprehend what could be a use case of something like
class SomeClass[A](a:A) {...}
When we pass a parameter of a known type to a function or a class, we know which operations are allowed on that parameter. So if I have a class as follows, I know that as the parameter is of type Int, I can perform '+' on the parameter 'a'
scala> class IntTest(a:Int) {
| def plusandPrintInt = {println(a+1)} //as 'a' is Int, we can do +
| }
defined class IntTest
scala> val i = new IntTest(1).plusandPrintInt
2
i: Unit = ()
But when I create a class which accepts type parameter [A], then the parameter could be of any type. Thus I do not what operations can be done on the passed parameter
scala> class TypeClass [A](a:A) {
| // what possibly can be done on A without knowing what A is?
| }
defined class TypeClass
Traits are different because we do not implement functions in trait but let other class do so. When a Trait which accepts Type parameter is extended, it is generally extended by specifying the real parameter type. Thus we can call specific operations on parameters as we know what the type of parameters are
scala> trait TraitClass [T] {
| def whatever (t:T) // I am not bothered as I do not need to implement this function
| }
defined trait TraitClass
scala> class extendTraitClass extends TraitClass[Int] {
| def whatever(t:Int) {println(t+1)} //I know that t is Int so I can use +
| }
defined class extendTraitClass
scala> (new extendTraitClass).whatever(1)
2
It seems the only operations available to 'a' are follows
scala> def someFunction[A](a:A) {
| a. //double tabbed to get this list
!= + == ensuring formatted hashCode toString
## -> asInstanceOf equals getClass isInstanceOf ?
Question 2 Is type class same as type trait?
Upvotes: 2
Views: 466
Reputation: 170735
"Type class" in Scala means something different from "a class which takes type parameter". It is a specific pattern which you shouldn't care about until you are comfortable with type parameters in general, so I'll consider your question 2 to be
is a class which takes type parameter the same as a trait which which takes type parameter
The answer is that there are of course differences between classes and traits (or there wouldn't be two different notions!), but these differences are the same independent of whether class/trait has type parameters.
Traits are different because we do not implement functions in trait but let other class do so.
No, you can implement methods in a trait. Here is an example:
trait Trait[T] {
def whatever(t: T) = Some(t)
}
When a Trait which accepts Type parameter is extended, it is generally extended by specifying the real parameter type.
Again, no:
trait Trait2[T] extends Trait[T] { ... }
is not problematic in any way. Neither is
class SomeClassForInt extends SomeClass[Int] { ... }
Upvotes: 0
Reputation: 149538
What is the use of a class which takes type parameter
A polymorphic class is useful when you can abstract over the type parameter. It means that the operations your class is exposing aren't specific to some concrete type.
Take the very common case of collections. If we look at a List[A]
for example, should the List[A]
be created for every type that list should hold? of course not, since the underlying operations can be performed for any type.
Another good example in Scala is Option[A]
. If you were the implementer, would you want to create an option for each possible value that exists? i.e:
class IntOption(a: Int)
class StringOption(a: String)
You wouldn't want to that because there is no reason. Since we can abstract over any type parameter A
, the class or trait can be made generic such that operations such as map
can take higher order functions of those abstract types.
Is type class same as type trait?
Not sure what you mean by "same as". classes and traits are different in nature and posses different qualities.
Upvotes: 1