m.hosseinian hat geschrieben:
Simply because it is explicitly mentioned:
Assume that you have a _base_ class Collection[A] which is covariant in A ...
This _base_ gives me enough clue that I have to extend from this Collection. Another suggestion is the name of asked trait "SortableCollection".
I think what is meant was that you have a baseclass Collection[+A] and you can extend it by creating a concrete Collection class like a List or an Array. The trait sortableCollection (because we have the template and strategy pattern) are what should be the interfaces that the collection depends on to implement the sorting algorithm. As was shown in the slides similar to here: http://i.imgur.com/wWp3mm7.png
where the Collection would be your Client.
m.hosseinian hat geschrieben:I cannot get your point here! You need to design a Collection with sorting capability, (it is able to sort its own elements and return a new object. Just like List.sort in Scala does!) with outsourced comparing function
The task (what I understood) was to make a Collection that is sortable with a SortableCollection trait based on the template or strategy pattern. If you keep a reference of this trait in your collection you can have it sorted by forwarding its sort call to the trait it currently references to. This way you can easily swap the referenced traits and sort by different criteria, (e.g bigger elements first, smaller elements first).
Say for example you have a concrete collection and your collection has a reference like this:
Code: Alles auswählen
var sorter: SortableCollection = new BiggerThanSortable()
your sort method inside Collection would look like this:
(Again, Generics aside)
you could also have a setSorter method where you change the sorter that the collection uses but you wouldn't have to change the sort method.
@Assax: By the way, your code would not compile, it will complain about undefined type parameter A.[/quote]
That's likely because I'm no good with generics in Scala at all. And honestly the fact that the scala introduction only had 1 slide of generics with the easiest example possible does not help while we had 10 slides of comparison between val var and def (hint: Feedback for the future).
I am having a hard time finding any good source on how to use Generics in Scala especially when it comes to extension of classes that also have a generic type declaration like
I agree though that it would be great if anyone official could clarify how to do this because this question has already been asked like 3-4 times and nobody is sure how to do it correctly.