8

I have method to which I pass an object. In this method I check it's type and depending on the type I do something with it and return a Long. I have tried every which way I can think of to do this and I always get several compiler errors telling me it expects a certain object but gets another. Can someone please explain to me what I am doing wrong and guide me in the right direction? What I have tried thus far is below:

  override def getInteger(obj:Object) = {
    if (obj.isInstanceOf[Object]) null
    else if (obj.isInstanceOf[Number]) 
      (obj:Number).longValue()
    else if (obj.isInstanceOf[Boolean]) 
      if (obj:Boolean) 1 else 0
    else if (obj.isInstanceOf[String]) 
      if ((obj:String).length == 0 | (obj:String) == "null") 
        null
      else
          try {
            Long.parse(obj:String)
          } catch {
            case e: Exception => throw new ValueConverterException("value \"" + obj.toString() + "\" of type " + obj.getClass().getName() + " is not convertible to Long")        
          }
  }

3 Answers 3

38

Pattern matching would make it much more nicer.

def getInteger(obj: Any) = obj match {
  case n: Number => n.longValue
  case b: Boolean => if(b) 1 else 0
  case s: String if s.length != 0 && s != "null" => s.toLong
  case _ => null
}
4
  • this worked out perfectly, with the exception that in the boolean case, i got a compiler error saying a long was expected. I took care of this by modifying it to if(b) 1.longValue else 0.longValue Apr 17, 2010 at 18:25
  • 3
    It's kinda crazy there is no implicit from int to long. It's ALWAYS safe to do that. Apr 17, 2010 at 18:37
  • 7
    @Russ: Or you could write if(b) 1L else 0L. Apr 17, 2010 at 18:40
  • if one types getInteger(3) then one gets an object having type Any, how to get it as in integer ? Sep 16, 2014 at 13:19
7

This code cries out for using a match:

obj match {
  case n: Number => n.longValue
  case b: Boolean => if (b) 1 else 0
  case s: String => if ((s eq null) || s.length == 0) null else {
            // try ... catch ... etc.
          }
  case o: Object => null
}

Followed my own advice from my comment to my original reply...

3
  • Of course, the problem with this is that with the Object case first, it will always match. It should be at to the end. Apr 17, 2010 at 17:43
  • This will not compile as Boolean is not subclass of Object. Apr 17, 2010 at 17:46
  • 1
    @Rahul G: That would be undiagnosed in the original but manifest in this case, a sign that using Scala's features help you expunge type errors. Apr 17, 2010 at 17:47
2

This might be a start:

def getInteger (o : Any) : Long = o match {
     case (o: Boolean) => if (o) 1 else 0       
     case (l: Long) => l                        
     case (s: String) => java.lang.Long.parseLong (s)   
     case _ => 0L 
}  

(I don't have something to override, and skipped the try/catch for brevity)

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.