How to call a method '! = 'For an object?

I just started playing with scala and used Michel Schinz's “Scala By Example” ( http://www.scala-lang.org/node/198 ) as a starting point. In the feature segment, I tried to use the reflection / method call to test the features and wanted to test the entire comparison operator. I got into a problem called mangling and found a solution in NameTransformer for operators. However, the operator! =, It seems to me that it does not translate into an equivalent function, as <, <=,>,> = is equal. I wonder if there is a way to call? = Like other operators that I did not find?

From pdf:

trait ord {
def < (that:Any):Boolean
def <= (that:Any):Boolean = (this < that) || (this == that)
def > (that:Any):Boolean = !(this <= that)
def >= (that:Any):Boolean = !(this < that)
}

class Date(y:Int, m:Int, d:Int) extends ord{
def year = y
def month = m
def day = d

override def toString():String = year + "-" + month + "-" + day

override def equals(that:Any): Boolean =
  that.isInstanceOf[Date] && {
    val o = that.asInstanceOf[Date]
    o.day == this.day && o.month == this.month && o.year == this.year
  }

override def <(that:Any):Boolean = {
  if (!that.isInstanceOf[Date])
    error("Cannot compare " + that + " and date")
  val o = that.asInstanceOf[Date]
  (year < o.year) ||
  (year == o.year && (month < o.month ||
    (month == o.month && day < o.day )))
}
}

My code is:

def Classes_Traits(){
val (d1, d2, d3)  = (new Date(2001, 10, 1), new Date(2001, 10, 1), new Date(2000, 1, 10))
println("d1 : " + d1)
println("d2 : " + d2)
println("d3 : " + d3)


Array((d1,d2), (d2,d3), (d3,d1)).foreach {
  (comp:(Date, Date)) =>
  println("comparing " + comp._1 + " and " + comp._2)
  val d = comp._1.getClass()
  Map(
       "equals            " -> "equals",
       "not equals        " -> "!=",
       "less than         " -> "<",
       "less than or equal" -> "<=",
       "more than         " -> ">",
       "more than or equal" -> ">=").foreach {
     (a) =>
       println(a._1 + " : " + 
       d.getMethod(scala.reflect.NameTransformer.encode(a._2), 
                   classOf[Object]).invoke(comp._1, comp._2))
       }
   /*println("equals : " + m.invoke(comp._1, comp._2) )
   // Same as above
   println(comp._1 + " == " + comp._2 + " is " + (comp._1 == comp._2))
   println(comp._1 + " != " + comp._2 + " is " + (comp._1 != comp._2))
   println(comp._1 + " > " + comp._2 + " is " + (comp._1 > comp._2))
   println(comp._1 + " >= " + comp._2 + " is " + (comp._1 >= comp._2))
   println(comp._1 + " < " + comp._2 + " is " + (comp._1 < comp._2))
   println(comp._1 + " <= " + comp._2 + " is " + (comp._1 <= comp._2))
   */
  }
}

exception: comparison of 2001-10-1 and 2001-10-1. greater than or equal to: true greater than: false

Exception in thread "main" java.lang.NoSuchMethodException:    proj2.Main$Date.$bang$eq(java.lang.Object)
    at java.lang.Class.getMethod(Class.java:1605)
    at proj2.Main$$anonfun$Classes_Traits$1$$anonfun$apply$1.apply(Main.scala:180)
    at proj2.Main$$anonfun$Classes_Traits$1$$anonfun$apply$1.apply(Main.scala:178)
    at scala.collection.immutable.HashMap$HashMap1.foreach(HashMap.scala:125)
    at scala.collection.immutable.HashMap$HashTrieMap.foreach(HashMap.scala:344)
    at proj2.Main$$anonfun$Classes_Traits$1.apply(Main.scala:177)
    at proj2.Main$$anonfun$Classes_Traits$1.apply(Main.scala:168)
    at scala.collection.IndexedSeqOptimized$class.foreach(IndexedSeqOptimized.scala:34)
    at scala.collection.mutable.ArrayOps.foreach(ArrayOps.scala:35)
    at proj2.Main$.Classes_Traits(Main.scala:167)
    at proj2.Main$.main(Main.scala:26)
    at proj2.Main.main(Main.scala)
+3
1

equals . == != Scala (, null.==(4) NullPointerException).

+5

All Articles