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 …
Have no idea what is or is not a good book. I can say that Programming In Scala is a real pain in the *** to read. And some of the author’s examples are just down-right impractical but hey … what do I know??? As for the overriding of hash-code and equals , I am not sure about that one yet. What I wanted to do was to have the Vertex identified only by the name component of that class. I am not sure what the underlying case class’s implementation of hashcode does. I did not wish for it to also use the weight component of the vertex because it is possible that the weight component will change for a node in some client level
)