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

Help on PartiallyOrdered[+A]

3 replies
normen.mueller
Joined: 2008-10-31,
User offline. Last seen 3 years 8 weeks ago.

He,

I am still trying to figure out the usage of trait
PartiallyOrdered[+A]. For example, I have the following class
hierarchy:
-------------------------------------------------------------------------------------------------------------
abstract class Library(val location: File)

class LibraryA(location: File, val code: Int)
extends Library(location)

class LibraryB(location: File, code: Int, val b: Boolean)
extends LibraryA(location, code) with PartiallyOrdered[LibraryB] {
def tryCompareTo [B >: LibraryB <% PartiallyOrdered[B]](that: B):
Option[Int] = {
if(that.isInstanceOf[LibraryB]) {
// comapare wrt. LibraryB attributes
None
} else None
}
}

class LibraryC(location: File, code: Int, b: Boolean, val s: String)
extends LibraryB(location, code, b) with PartiallyOrdered[LibraryC] {
override def tryCompareTo [B >: LibraryC <% PartiallyOrdered[B]]
(that: B): Option[Int] = {
if(that.isInstanceOf[LibraryC]) {
// compare wrt. LibraryC attributes, e.g.
that.asInstanceOf[LibraryC].s < this.s
None
} else if(that.isInstanceOf[LibraryB]) that tryCompareTo this
else None
}
}

object MyPartiallyOrderedExample {
def main(args: Array[String]): Unit = {
val a0 = new LibraryA(new File("foo"), 1)

val b0 = new LibraryB(new File("bar"), 2, true)
val b1 = new LibraryB(new File("bar"), 3, true)

val c0 = new LibraryC(new File("faz"), 3, false, "hello")
val c1 = new LibraryC(new File("faz"), 4, false, "world!")

println(b0 < b1)
println(c0 < c1)
println(b0 < c0)

//println(a0 < b0) // LibraryA is not partially ordered!
}
}
-------------------------------------------------------------------------------------------------------------
I don't quite understand the signature of tryCompareTo:

def tryCompareTo [B >: A <% PartiallyOrdered[B]](that: B): Option[Int]

Why does tryCompareTo accept values of supertypes of A?

In other words: Implementing tryCompareTo in LibraryC, for example,
do I have to use "isInstanceOf" and "asInstanceOf", respectively, if I
want to compare two LibraryC instances wrt. their attributes? And is
it also correct if the "that" instance is a supertype of "this" and
also partially ordered (which is required by the signature of
tryCompareTo, I guess) to pass the comparison to the "that" instance?

Your see, I am not yet familiar with this trait, but I really want to
use it. So, could please someone explain the signature/ usage (maybe
with a better example) of this trait?

Cheers,
--
Normen Müller

Carsten Saager
Joined: 2008-12-19,
User offline. Last seen 42 years 45 weeks ago.
Re: Help on PartiallyOrdered[+A]
PartiallyOrdered is covariant and you need it to accept in contravariant position super types:

val a:MyPOtype = getBasic
val b:SubOfMyPOType = getSpecial

b tryCompareTo a //would be an error if SubOfMyPOType would accept a super

/Carsten

On Mon, Jan 19, 2009 at 9:43 AM, Normen Mueller <normen [dot] mueller [at] googlemail [dot] com> wrote:
He,

I am still trying to figure out the usage of trait PartiallyOrdered[+A].  For example, I have the following class hierarchy:
-------------------------------------------------------------------------------------------------------------
abstract class Library(val location: File)

class LibraryA(location: File, val code: Int)
       extends Library(location)

class LibraryB(location: File, code: Int, val b: Boolean)
       extends LibraryA(location, code) with PartiallyOrdered[LibraryB] {
 def tryCompareTo [B >: LibraryB <% PartiallyOrdered[B]](that: B): Option[Int] = {
   if(that.isInstanceOf[LibraryB]) {
     // comapare wrt. LibraryB attributes
     None
   } else None
 }
}

class LibraryC(location: File, code: Int, b: Boolean, val s: String)
       extends LibraryB(location, code, b) with PartiallyOrdered[LibraryC] {
 override def tryCompareTo [B >: LibraryC <% PartiallyOrdered[B]](that: B): Option[Int] = {
   if(that.isInstanceOf[LibraryC]) {
     // compare wrt. LibraryC attributes, e.g. that.asInstanceOf[LibraryC].s < this.s
     None
   } else if(that.isInstanceOf[LibraryB]) that tryCompareTo this
     else None
 }
}

object MyPartiallyOrderedExample {
 def main(args: Array[String]): Unit = {
   val a0 = new LibraryA(new File("foo"), 1)

   val b0 = new LibraryB(new File("bar"), 2, true)
   val b1 = new LibraryB(new File("bar"), 3, true)

   val c0 = new LibraryC(new File("faz"), 3, false, "hello")
   val c1 = new LibraryC(new File("faz"), 4, false, "world!")

   println(b0 < b1)
   println(c0 < c1)
   println(b0 < c0)

   //println(a0 < b0) // LibraryA is not partially ordered!
 }
}
-------------------------------------------------------------------------------------------------------------
I don't quite understand the signature of tryCompareTo:

def tryCompareTo [B >: A <% PartiallyOrdered[B]](that: B): Option[Int]

Why does tryCompareTo accept values of supertypes of A?

In other words:  Implementing tryCompareTo in LibraryC, for example, do I have to use "isInstanceOf" and "asInstanceOf", respectively, if I want to compare two LibraryC instances wrt. their attributes?  And is it also correct if the "that" instance is a supertype of "this" and also partially ordered (which is required by the signature of tryCompareTo, I guess) to pass the comparison to the "that" instance?

Your see, I am not yet familiar with this trait, but I really want to use it.  So, could please someone explain the signature/ usage (maybe with a better example) of this trait?

Cheers,
--
Normen Müller

normen.mueller
Joined: 2008-10-31,
User offline. Last seen 3 years 8 weeks ago.
Re: Help on PartiallyOrdered[+A]

On Jan 19, 2009, at 9:58 AM, Carsten Saager wrote:
> PartiallyOrdered is covariant and you need it to accept in
> contravariant position super types:
>
> val a:MyPOtype = getBasic
> val b:SubOfMyPOType = getSpecial
>
> b tryCompareTo a //would be an error if SubOfMyPOType would accept a
> super

"SubOfMyPOType" is a sub-type of "MyPOType", so it accepts a super type?

Anyway, as I am still learning on type variances, my major question is
regarding "isInstanceOf" and "asInstanceOf", respectively, in my below
example. Is this the correct approach? In your example this would
mean sth. like:

class SubOfMyPOType extends MyPOType with
PartiallyOrdered[SubOfMyPOType] {
def tryCompareTo [B >: SubOfMyPOType <% PartiallyOrdered[B]](that:
B): Option[Int] = {
if(that.isInstanceOf[SubOfMyPOType]) {
val other = that.asInstanceOf[SubOfMyPOType]

// compare this and other wrt. some instance variables
}
}
}

> On Mon, Jan 19, 2009 at 9:43 AM, Normen Mueller > wrote:
> I am still trying to figure out the usage of trait
> PartiallyOrdered[+A]. For example, I have the following class
> hierarchy:
> -------------------------------------------------------------------------------------------------------------
> abstract class Library(val location: File)
>
> class LibraryA(location: File, val code: Int)
> extends Library(location)
>
> class LibraryB(location: File, code: Int, val b: Boolean)
> extends LibraryA(location, code) with
> PartiallyOrdered[LibraryB] {
> def tryCompareTo [B >: LibraryB <% PartiallyOrdered[B]](that: B):
> Option[Int] = {
> if(that.isInstanceOf[LibraryB]) {
> // comapare wrt. LibraryB attributes
> None
> } else None
> }
> }
>
> class LibraryC(location: File, code: Int, b: Boolean, val s: String)
> extends LibraryB(location, code, b) with
> PartiallyOrdered[LibraryC] {
> override def tryCompareTo [B >: LibraryC <% PartiallyOrdered[B]]
> (that: B): Option[Int] = {
> if(that.isInstanceOf[LibraryC]) {
> // compare wrt. LibraryC attributes, e.g.
> that.asInstanceOf[LibraryC].s < this.s
> None
> } else if(that.isInstanceOf[LibraryB]) that tryCompareTo this
> else None
> }
> }
>
> object MyPartiallyOrderedExample {
> def main(args: Array[String]): Unit = {
> val a0 = new LibraryA(new File("foo"), 1)
>
> val b0 = new LibraryB(new File("bar"), 2, true)
> val b1 = new LibraryB(new File("bar"), 3, true)
>
> val c0 = new LibraryC(new File("faz"), 3, false, "hello")
> val c1 = new LibraryC(new File("faz"), 4, false, "world!")
>
> println(b0 < b1)
> println(c0 < c1)
> println(b0 < c0)
>
> //println(a0 < b0) // LibraryA is not partially ordered!
> }
> }
> -------------------------------------------------------------------------------------------------------------
> I don't quite understand the signature of tryCompareTo:
>
> def tryCompareTo [B >: A <% PartiallyOrdered[B]](that: B): Option[Int]
>
> Why does tryCompareTo accept values of supertypes of A?
>
> In other words: Implementing tryCompareTo in LibraryC, for example,
> do I have to use "isInstanceOf" and "asInstanceOf", respectively, if
> I want to compare two LibraryC instances wrt. their attributes? And
> is it also correct if the "that" instance is a supertype of "this"
> and also partially ordered (which is required by the signature of
> tryCompareTo, I guess) to pass the comparison to the "that" instance?
>
> Your see, I am not yet familiar with this trait, but I really want
> to use it. So, could please someone explain the signature/ usage
> (maybe with a better example) of this trait?
>
> Cheers,
> --
> Normen Müller
>

Cheers,
--
Normen Müller

normen.mueller
Joined: 2008-10-31,
User offline. Last seen 3 years 8 weeks ago.
Re: Help on PartiallyOrdered[+A]

Is pattern matching the answer/ nice approach?

Sth. like:

abstract class Library(val location: File)

class LibraryA(location: File, val code: Int) extends Library(location)

class LibraryB(location: File, code: Int, val b: Boolean)
extends LibraryA(location, code) with PartiallyOrdered[LibraryB] {

def tryCompareTo [B >: LibraryB <% PartiallyOrdered[B]](that: B):
Option[Int] = that match {
case o: LibraryB => ...
case _ => that tryCompareTo this
}
}
class LibraryC(location: File, code: Int, b: Boolean, val s: String)
extends LibraryB(location, code, b) with PartiallyOrdered[LibraryC] {

override def tryCompareTo [B >: LibraryC <% PartiallyOrdered[B]]
(that: B): Option[Int] = that match {
case o: LibraryC => this.s compareTo o.s
case o: LibraryB => that tryCompareTo this // maybe this case
should be the default like in LibraryB??!!
case _ => None
}
}

On Jan 19, 2009, at 9:43 AM, Normen Mueller wrote:
> I am still trying to figure out the usage of trait
> PartiallyOrdered[+A]. For example, I have the following class
> hierarchy:
> -------------------------------------------------------------------------------------------------------------
> abstract class Library(val location: File)
>
> class LibraryA(location: File, val code: Int)
> extends Library(location)
>
> class LibraryB(location: File, code: Int, val b: Boolean)
> extends LibraryA(location, code) with PartiallyOrdered[LibraryB] {
> def tryCompareTo [B >: LibraryB <% PartiallyOrdered[B]](that: B):
> Option[Int] = {
> if(that.isInstanceOf[LibraryB]) {
> // comapare wrt. LibraryB attributes
> None
> } else None
> }
> }
>
> class LibraryC(location: File, code: Int, b: Boolean, val s: String)
> extends LibraryB(location, code, b) with PartiallyOrdered[LibraryC] {
> override def tryCompareTo [B >: LibraryC <% PartiallyOrdered[B]]
> (that: B): Option[Int] = {
> if(that.isInstanceOf[LibraryC]) {
> // compare wrt. LibraryC attributes, e.g.
> that.asInstanceOf[LibraryC].s < this.s
> None
> } else if(that.isInstanceOf[LibraryB]) that tryCompareTo this
> else None
> }
> }
>
> object MyPartiallyOrderedExample {
> def main(args: Array[String]): Unit = {
> val a0 = new LibraryA(new File("foo"), 1)
>
> val b0 = new LibraryB(new File("bar"), 2, true)
> val b1 = new LibraryB(new File("bar"), 3, true)
>
> val c0 = new LibraryC(new File("faz"), 3, false, "hello")
> val c1 = new LibraryC(new File("faz"), 4, false, "world!")
>
> println(b0 < b1)
> println(c0 < c1)
> println(b0 < c0)
>
> //println(a0 < b0) // LibraryA is not partially ordered!
> }
> }
> -------------------------------------------------------------------------------------------------------------
> I don't quite understand the signature of tryCompareTo:
>
> def tryCompareTo [B >: A <% PartiallyOrdered[B]](that: B): Option[Int]
>
> Why does tryCompareTo accept values of supertypes of A?
>
> In other words: Implementing tryCompareTo in LibraryC, for example,
> do I have to use "isInstanceOf" and "asInstanceOf", respectively, if
> I want to compare two LibraryC instances wrt. their attributes? And
> is it also correct if the "that" instance is a supertype of "this"
> and also partially ordered (which is required by the signature of
> tryCompareTo, I guess) to pass the comparison to the "that" instance?
>
> Your see, I am not yet familiar with this trait, but I really want
> to use it. So, could please someone explain the signature/ usage
> (maybe with a better example) of this trait?

Cheers,
--
Normen Müller

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