Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5999831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:36:28+00:00 2026-05-23T00:36:28+00:00

I just started playing with scala and have been using the Scala By Example

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T00:36:29+00:00Added an answer on May 23, 2026 at 12:36 am

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.