Hello again.
I’m working through working though Horstrmann’s Scala for the Impatient and he gives the problem in chapter 10:
2. Define a class OrderedPoint by mixing scala.math.Ordered[Point] into java.awt.Point. Use lexicographic ordering, i.e. (x,y) < (x’,y’) if x < x’ or x = x’ and y < y’.
Following the example in Odersky (p225):
class Rational(n: Int, d: Int) extends Ordered[Rational] {
// ...
def compare(that: Rational) = (this.numer * that.denom) - (that.numer * this.denom)
}
I tried several variations like:
class OrderedPoint(x : Int, y: Int) extends scala.math.Ordered[java.awt.Point] {
def compare(that: OrderedPoint ) = if(this.x == that.x) this.y-that.y else
this.x-that.x
}
But they all give errors. This version has three errors:
error: type mismatch;
found : that.type (with underlying type this.OrderedPoint)
required: ?{def x: ?}
Note that implicit conversions are not applicable because they are ambiguous:
both method any2Ensuring in object Predef of type [A](x: A)Ensuring[A]
and method any2ArrowAssoc in object Predef of type [A](x: A)ArrowAssoc[A]
are possible conversion functions from that.type to ?{def x: ?}
def compare(that: OrderedPoint ) = if(this.x == that.x) this.y-that.y else
^
C:\Users\Mike\IdeaProjects\sfti10\sfti10ex2.scala:10: error: value y is not a me
mber of this.OrderedPoint
def compare(that: OrderedPoint ) = if(this.x == that.x) this.y-that.y else
^
C:\Users\Mike\IdeaProjects\sfti10\sfti10ex2.scala:11: error: type mismatch;
found : that.type (with underlying type this.OrderedPoint)
required: ?{def x: ?}
Note that implicit conversions are not applicable because they are ambiguous:
both method any2Ensuring in object Predef of type [A](x: A)Ensuring[A]
and method any2ArrowAssoc in object Predef of type [A](x: A)ArrowAssoc[A]
are possible conversion functions from that.type to ?{def x: ?}
this.x-that.x
^
I don’t understand the error messages. I have no idea what the first is trying to say.
For the second and third, in the REPL I can do:
scala> val p1 = new java.awt.Point(1,1)
p1: java.awt.Point = java.awt.Point[x=1,y=1]
scala> p1.x
res0: Int = 1
scala> p1.y
res1: Int = 1
scala>
So I think .x and .y are members of java.awt.Point so I don’t understand why they wouldn’t be members of OrderedPoint.
Thanks for the help,
Mike
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Thanks for the answers. I was able to figure this out with the help. It turns out there were two errors that were making me confused. First, the parameter in scala.math.Ordered[] should be the class name, in this case “OrderedPoint”. Once I fixed that, then the fact that x and y were not declared with val prevented the that.x and that.y from working, even though they are used in the compare so I thought they would be private members of the class. The fact that they are not public doesn’t matter. The following class definition works:
class OrderedPoint(private val x : Int, private val y: Int) extends scala.math.Ordered[OrderedPoint] {
def compare(that: OrderedPoint ) = if(this.x == that.x) this.y-that.y else
this.x-that.x
}
Obviously, it works without the “private” for x and y as well.
Thanks,
Mike