3

I just started playing with scala and have been using the "Scala By Example" by Michel Schinz ( http://www.scala-lang.org/node/198 ) as a starting point. In the segment on traits, I've attempted to use reflection/method invocation to test traits and wanted to test all the comparison operator. I hit the issue with name mangling and found the solution in NameTransformer for the operators. However, the != operator, appears to me that it does not translate into an equivalent function like <, <=, >, >=, equals. I wonder if there is a way to invoke != much like the other operators that I have not found out?

From the 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:

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: comparing 2001-10-1 and 2001-10-1 more than or equal : true more 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)

1 Answer 1

5

Try calling equals and negating the result. == and != benefit from a little compiler magic in Scala (e.g., you can call null.==(4) without getting a NullPointerException).

2
  • Thanks. I'll read up more on special compiler attention to == and !=. Pointers on where to read up on them are welcome. Jun 13, 2011 at 15:59
  • I’d say that for those matters, compiling small snippets and looking at the generated bytecode is the best way to understand the compiler. Alternatively, check out the source code of the compiler… Jun 13, 2011 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.