dhg
dhg

Reputation: 52691

Scala: checking if an object is Numeric

Is it possible for a pattern match to detect if something is a Numeric? I want to do the following:

class DoubleWrapper(value: Double) {
  override def equals(o: Any): Boolean = o match {
    case o: Numeric => value == o.toDouble
    case _ => false
  }
  override def hashCode(): Int = value ##
}

But of course this doesn't really work because Numeric isn't the supertype of things like Int and Double, it's a typeclass. I also can't do something like def equals[N: Numeric](o: N) because o has to be Any to fit the contract for equals.

So how do I do it without listing out every known Numeric class (including, I guess, user-defined classes I may not even know about)?

Upvotes: 14

Views: 2985

Answers (3)

vegabondx
vegabondx

Reputation: 71

I would suggest to use java.lang.Number instead.

class DoubleWrapper(value: Double) {
  override def equals(o: Any): Boolean = o match {
    case o: Number => value == o.doubleValue()
    case _ => false
  }
  override def hashCode(): Int = value ##
}

I believe it works because the Double is from java.lang and therefore it connects better with Number. Numeric needs a TypeTag which is tricky if you want to work with Any.

Upvotes: 0

Kim Stebel
Kim Stebel

Reputation: 42047

The original problem is not solvable, and here is my reasoning why:

To find out whether a type is an instance of a typeclass (such as Numeric), we need implicit resolution. Implicit resolution is done at compile time, but we would need it to be done at runtime. That is currently not possible, because as far as I can tell, the Scala compiler does not leave all necessary information in the compiled class file. To see that, one can write a test class with a method that contains a local variable, that has the implicit modifier. The compilation output will not change when the modifier is removed.

Upvotes: 5

0__
0__

Reputation: 67310

Are you using DoubleWrapper to add methods to Double? Then it should be a transparent type, i.e. you shouldn't be keeping instances, but rather define the pimped methods to return Double instead. That way you can keep using == as defined for primitives, which already does what you want (6.0 == 6 yields true).


Ok, so if not, how about

override def equals(o: Any): Boolean = o == value

If you construct equals methods of other wrappers accordingly, you should end up comparing the primitive values again.

Another question is whether you should have such an equals method for a stateful wrapper. I don't think mutable objects should be equal according to one of the values they hold—you will most likely run into trouble with that.

Upvotes: 0

Related Questions