1

I want to check whether a obj in Scala is of type Array, not caring about whether what type of the values are inside. Is there some easy way to do this?

var x = Array(1,2,3)
x.isInstanceOf[Array[Any]] // which is false

I want it to be true.

Update:

I have a function:

def someFunction(obj: Any) {
    if (obj.isInstanceOf[Int]) {
        // do something
    }
    if (obj.isInstanceOf[String]) {
        // do something
    }
    if (obj.isInstanceOf[Array[Any]) {
        // for each of the obj inside the Array do something
    }
}

2 Answers 2

7

array Array(1,2,3) is of type Int, so Array[Any] would weirdly return false only in case of Array collection.

If you don't care the type of Array, you can compare to Array[_] (array of whatever type)

scala> var x = Array(1,2,3)
x: Array[Int] = Array(1, 2, 3)

scala> x.isInstanceOf[Array[Int]]
res0: Boolean = true

scala> x.isInstanceOf[Array[_ <: Any]]
res7: Boolean = true

scala> x.isInstanceOf[Array[_ <: AnyVal]]
res12: Boolean = true

scala> x.isInstanceOf[Array[_]]
res13: Boolean = true

scala.Int extends AnyVal which extends Any you can explicitly mentions _ extends AnyVal.

But other scala collections like List[T] is instance of List[Any]

scala> List(1, 2).isInstanceOf[List[Any]]
res30: Boolean = true

scala> Seq(1, 2).isInstanceOf[Seq[Any]]
res33: Boolean = true

Also, List[T] is instance of Seq[Any], because List extends Seq, and T extends Any

scala> List(1, 2).isInstanceOf[Seq[Any]]
res35: Boolean = true

AND, for if else you can solve with pattern match way,

array match {
   case x: Int => println("int") 
   case x: String => "string" 
   case Array[Int] => println("ints")
   case Array[String] => println("strings")
   case _ => println("whatever")
 }
Sign up to request clarification or add additional context in comments.

2 Comments

There is a problem with you List example. It will always return true no matter what type you use for T because of erasure. List(1,2,3).isInstanceOf[List[String]] returns true.
You are very right because its matching the erasure, even List(1,2,3).isInstanceOf[List[Map[String, String]]] returns true, but compiler will warn you that List[Int] cannot also be a List[String].
3
x.isInstanceOf[Array[_]]

use _ for container any type match. and for _ it's called existential type.

Document:

Underscore for existential type in Scala

2 Comments

Thank you. It works! But I am a little bit confused about the design. It is more intuitive for beginner to use Any here but why we use underscore. I think every beginner will try Array[Any] first instead of Array[_]
I'm not sure why it is more intuitive to use Any. An Array[X] is an Array[X], it is not an Array[Y]. An Array[Int] is an Array[Int], it is not an Array[Any]. Array[Int] and Array[Any] are two completely different types, they have absolutely nothing to do with each other. _ is used as a wildcard character all over Scala, why would it be confusing to use it as a wildcard character here?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.