I am “trying” to teach myself scala by reading the book, Programming in Scala, 3rd Edition. I am a java developer professionally. I am trying to write a generic scala case class. I guess I am approaching it from a Java Mind Set. So far cannot get it to work properly. Here is my issue and also what I am attempting to do:
I wish to create a generic class that takes type parameters:
case class bob[T,W]( name: T , weight: W) {
I want this part of the class to override hashcode and equals. I also want
this part of the class to implement sorting .
}
Now … this is relatively simple to do in java. But I am having one seriously hard time doing this in Scala. You see, if I create a list of objects of type Bob , I wish for them to be sorted by some other type that sorts them, such as a Binary Trees class, or passing to a sort method. I know with my limited knowledge of scala at this time, I am probably doing everything completely wrong.
For example in java, it is just a simple matter of doing something like this:
class Bob<T extends Comparable<? super T> , W> extends Comparable {
and in here you just override the compareTo method in java. It is very simple.
}
With that I can create objects of type Bob and use different types that are Comparable. I am trying to do the same thing in Scala.
Maybe some one knows how to create a sortable generic class in Scala and can help me with this or point me in the right direction.
Thanks …