How to test if object is a numeric type?

3,395 views
Skip to first unread message

Michi

unread,
Feb 19, 2011, 9:26:12 AM2/19/11
to scala-user
Hi,

is there an easy way in Scala to test if an object is a numeric type?
In Java, I could do the following

Object value = ...
if (value instanceof Number)
{
Number number = (Number)value;
dblVal = number.doubleValue();
... // do some calculations here
}

As far as I can see, there is no interface implemented by all numeric
types. So how can I write the above code in Scala?

Thanks,
Michael

HamsterofDeath

unread,
Feb 19, 2011, 9:37:28 AM2/19/11
to scala...@googlegroups.com
if the source is any/an object -> pattern matching.
passing objects around is a bad idea in general.

Michi

unread,
Feb 19, 2011, 9:43:12 AM2/19/11
to scala-user
Hi,

> if the source is any/an object -> pattern matching.
> passing objects around is a bad idea in general.

I found the solution:

val v: Any = 3.14
v match {
case number: java.lang.Number => println("Number")
case _ => println("Not a number")
}

I did not that know Scala numeric types implement the java.lang.Number
interface. That's not mentioned in the API documentation.

Thanks,
Michael

Jason Zaugg

unread,
Feb 19, 2011, 9:44:31 AM2/19/11
to Michi, scala-user
On Sat, Feb 19, 2011 at 3:26 PM, Michi
<michael...@physik.tu-muenchen.de> wrote:
> Hi,
>
> is there an easy way in Scala to test if an object is a numeric type?
> In Java, I could do the following
>
> Object value = ...
> if (value instanceof Number)
> {
>  Number number = (Number)value;
>  dblVal = number.doubleValue();
>  ... // do some calculations here
> }

If you have an AnyRef (the scala equivalent of java.lang.Object), you
can just check value.isInstanceOf[Number], as you've already done.

If you have an Any (a Scala-only type that encompasses primitives and
reference types), you can do something like this:

scala> def asNumber(a: Any) = a.asInstanceOf[AnyRef] match {
case n : Number => Some(n)
case _ => None
}

asNumber: (a: Any)Option[java.lang.Number]

scala> asNumber(1)
res6: Option[java.lang.Number] = Some(1)

scala> asNumber(Int.box(1))
res7: Option[java.lang.Number] = Some(1)

scala> asNumber("1")
res8: Option[java.lang.Number] = None

It's always safe to call .asInstanceOf[AnyRef], which will box
primitives, i.e. wrap a Int in a java.lang.Integer.

But if you provide a but more context of what you're trying to do, we
might find an even better solution.

-jason

Jesper de Jong

unread,
Feb 21, 2011, 6:06:56 AM2/21/11
to scala...@googlegroups.com
If your reason for doing this is that you want to write some method that works for anything that can be seen as a Double, then you can use a view bound:

def someMethod[N <% Double](value: N) = ... // do some calculation with value as if it is a Double

Jesper



--
Jesper de Jong
jes...@gmail.com



--
Jesper de Jong
jes...@gmail.com

Sonnenschein

unread,
Feb 22, 2011, 8:14:57 AM2/22/11
to scala...@googlegroups.com
> Hi,
> is there an easy way in Scala to test if an object is a numeric type?

maybe
isInstanceOf[java.lang.Number]
resp.
o match { case n: Number =>
helps you

Reply all
Reply to author
Forward
0 new messages