This page is no longer maintained — Please continue to the home page at www.scala-lang.org

abstract class and the ordered trait

5 replies
Thomas Schank
Joined: 2009-07-14,
User offline. Last seen 42 years 45 weeks ago.

Hi, I got stuck with the following:

abstract class AbstractClass extends Ordered[AbstractClass]

class GenuineClass extends AbstractClass

object GenuineClass extends Application{
val m = new TreeMap[GenuineClass,Double]
}

will give an error:
no implicit argument matching parameter type (GenuineClass) =>
Ordered[GenuineClass] was found

however,

class GenuineClass extends Ordered[GenuineClass]

val m = new TreeMap[GenuineClass,Double]

would compile.

Why doesn't the first example complile and how could it be fixed?

thanks, Thomas

Kevin Wright
Joined: 2009-06-09,
User offline. Last seen 49 weeks 3 days ago.
Re: abstract class and the ordered trait
The error is exactly right.

For a Map[K, V], the key type K has to implement Ordered[K]
As Ordered isn't covariant in K, then Ordered[S] can't be used as Ordered[K] for some S that's a supertype of K

Also, the Ordered trait has an abstract method "compare" that you need to implement.  Without this GenuineClass isn't valid unless it's also declared abstract, and it can't be instantiated.


The solution is:

    abstract class AbstractClass

    class GenuineClass extends AbstractClass with Ordered[GenuineClass] {
       def compare(that : GenuineClass) : Int = <do something with this and that>
    }

    object GenuineClass extends Application {
       val m = new TreeMap[GenuineClass,Double]
    }


See: http://scala-tools.org/scaladocs/scala-library/2.7.1/scala/Ordered.html

On Tue, Jul 14, 2009 at 9:01 AM, Thomas Schank <thschank09 [at] gmail [dot] com> wrote:


Hi, I got stuck with the following:

 abstract class AbstractClass extends Ordered[AbstractClass]

 class GenuineClass extends AbstractClass

 object GenuineClass extends Application{
   val m = new TreeMap[GenuineClass,Double]
 }


will give an error:
 no implicit argument matching parameter type (GenuineClass) =>
Ordered[GenuineClass] was found

however,

 class GenuineClass extends Ordered[GenuineClass]

 val m = new TreeMap[GenuineClass,Double]

would compile.

Why doesn't the first example complile and how could it be fixed?

thanks, Thomas


--
View this message in context: http://www.nabble.com/abstract-class-and-the-ordered-trait-tp24475270p24475270.html
Sent from the Scala - User mailing list archive at Nabble.com.


Joshua.Suereth
Joined: 2008-09-02,
User offline. Last seen 32 weeks 5 days ago.
Re: abstract class and the ordered trait
I believe you could also do:

abstract class AbstractClass[A] extends Ordered[A] {
  ...
}

class GenuineClass extends AbstractClass[GenuineClass] {

}


For even more type strictness, you could force A to be a subclass of AbstractClass.


- Josh

On Tue, Jul 14, 2009 at 4:31 AM, Kevin Wright <kev [dot] lee [dot] wright [at] googlemail [dot] com> wrote:
The error is exactly right.

For a Map[K, V], the key type K has to implement Ordered[K]
As Ordered isn't covariant in K, then Ordered[S] can't be used as Ordered[K] for some S that's a supertype of K

Also, the Ordered trait has an abstract method "compare" that you need to implement.  Without this GenuineClass isn't valid unless it's also declared abstract, and it can't be instantiated.


The solution is:

    abstract class AbstractClass

    class GenuineClass extends AbstractClass with Ordered[GenuineClass] {
       def compare(that : GenuineClass) : Int = <do something with this and that>
    }

    object GenuineClass extends Application {
       val m = new TreeMap[GenuineClass,Double]
    }


See: http://scala-tools.org/scaladocs/scala-library/2.7.1/scala/Ordered.html

On Tue, Jul 14, 2009 at 9:01 AM, Thomas Schank <thschank09 [at] gmail [dot] com> wrote:


Hi, I got stuck with the following:

 abstract class AbstractClass extends Ordered[AbstractClass]

 class GenuineClass extends AbstractClass

 object GenuineClass extends Application{
   val m = new TreeMap[GenuineClass,Double]
 }


will give an error:
 no implicit argument matching parameter type (GenuineClass) =>
Ordered[GenuineClass] was found

however,

 class GenuineClass extends Ordered[GenuineClass]

 val m = new TreeMap[GenuineClass,Double]

would compile.

Why doesn't the first example complile and how could it be fixed?

thanks, Thomas


--
View this message in context: http://www.nabble.com/abstract-class-and-the-ordered-trait-tp24475270p24475270.html
Sent from the Scala - User mailing list archive at Nabble.com.



Thomas Schank
Joined: 2009-07-14,
User offline. Last seen 42 years 45 weeks ago.
Re: abstract class and the ordered trait

>> As Ordered isn't covariant in K, then Ordered[S] can't be used as
>> Ordered[K] for some S that's a supertype of K

this explains it;

However, my situation is a little bit more complex. I'm actually
doing something similar that Josh proposes; I use a abstract class
(Now, this is copy and paste from original code; hence the weird names,
sorry):

abstract class PhysicalValue[X <: PhysicalValue[X]](val value: Double)
extends Ordered[PhysicalValue[X]] { ....

and I want to use devied classes in some other class

class ContinousFunctionOfSegments[X <: PhysicalValue[X], Y <:
PhysicalValue[Y]](val segments : TreeMap[X,Y]) {
def this() = this(new TreeMap[X,Y]())
.....

Now, I'll get the same error e.g. in the line with the auxillary
constructor.
Is there a way I can specify X to be Ordered without referencing a actuall
derived class (which is the point of generics I belive).

Thomas

>
>
>
>
Josh Suereth wrote:
>
>>I believe you could also do:
>>
>>abstract class AbstractClass[A] extends Ordered[A] {
>> ...
>>}
>>
>>class GenuineClass extends AbstractClass[GenuineClass] {
>>
>>}
>>
>>
>>For even more type strictness, you could force A to be a subclass of
>>AbstractClass.
>>
>>
>>- Josh
>>
>>On Tue, Jul 14, 2009 at 4:31 AM, Kevin Wright
>> wrote:
>>
>>> The error is exactly right.
>>>
>>> For a Map[K, V], the key type K has to implement Ordered[K]
>>> As Ordered isn't covariant in K, then Ordered[S] can't be used as
>>> Ordered[K] for some S that's a supertype of K
>>>
>>> Also, the Ordered trait has an abstract method "compare" that you need
>>> to
>>> implement. Without this GenuineClass isn't valid unless it's also
>>> declared
>>> abstract, and it can't be instantiated.
>>>
>>>
>>> The solution is:
>>>
>>> abstract class AbstractClass
>>>
>>> class GenuineClass extends AbstractClass with Ordered[GenuineClass]
>>> {
>>> def compare(that : GenuineClass) : Int = >> and
>>> that>
>>> }
>>>
>>> object GenuineClass extends Application {
>>> val m = new TreeMap[GenuineClass,Double]
>>> }
>>>
>>>
>>> See:
>>> http://scala-tools.org/scaladocs/scala-library/2.7.1/scala/Ordered.html
>>>
>>>
>>> On Tue, Jul 14, 2009 at 9:01 AM, Thomas Schank
>>> wrote:
>>>
>>>>
>>>>
>>>> Hi, I got stuck with the following:
>>>>
>>>> abstract class AbstractClass extends Ordered[AbstractClass]
>>>>
>>>> class GenuineClass extends AbstractClass
>>>>
>>>> object GenuineClass extends Application{
>>>> val m = new TreeMap[GenuineClass,Double]
>>>> }
>>>>
>>>>
>>>> will give an error:
>>>> no implicit argument matching parameter type (GenuineClass) =>
>>>> Ordered[GenuineClass] was found
>>>>
>>>> however,
>>>>
>>>> class GenuineClass extends Ordered[GenuineClass]
>>>>
>>>> val m = new TreeMap[GenuineClass,Double]
>>>>
>>>> would compile.
>>>>
>>>> Why doesn't the first example complile and how could it be fixed?
>>>>
>>>> thanks, Thomas
>>>>
>>>>
>>>> --
>>>> View this message in context:
>>>> http://www.nabble.com/abstract-class-and-the-ordered-trait-tp24475270p24...
>>>> Sent from the Scala - User mailing list archive at Nabble.com.
>>>>
>>>>
>>>
>>
>>

Colin Bullock
Joined: 2009-01-23,
User offline. Last seen 42 years 45 weeks ago.
Re: abstract class and the ordered trait
This should work fine if you change:


abstract class PhysicalValue[X <: PhysicalValue[X]](val value: Double)
extends Ordered[PhysicalValue[X]] { ....

to

abstract PhysicalValue[X <: PhysicalValue[X]](val value: Double) extends Ordered[X] { ... }

- Colin

Thomas Schank
Joined: 2009-07-14,
User offline. Last seen 42 years 45 weeks ago.
Re: abstract class and the ordered trait

>abstract PhysicalValue[X <: PhysicalValue[X]](val value: Double) extends
>Ordered[X] { ... }

indeed it does; it means that I'll have to implement compare for any of
the derived classes, a little nuisance I'm willing to accept

Thanks !

>
>
Colin Bullock wrote:
>
>>This should work fine if you change:
>>
>>
>>> abstract class PhysicalValue[X <: PhysicalValue[X]](val value: Double)
>>> extends Ordered[PhysicalValue[X]] { ....
>>>
>>
>>to
>>
>>abstract PhysicalValue[X <: PhysicalValue[X]](val value: Double) extends
>>Ordered[X] { ... }
>>
>>- Colin
>>
>>

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland