6

Is there a simple way to determine if a variable is a list, dictionary, or something else? Basically I am getting an object back that may be either type and I need to be able to tell the difference.

In Python, we have "Type()", "Typeof()" that

scala> val c: String = "Hello world"

Is there any way to determine : Typeof(c) to print : String

user3378649
  • 5,154
  • 14
  • 52
  • 76
  • 2
    Don't do this! Let the compiler worry about types for you! Life can be so much better! – Travis Brown Mar 09 '14 at 00:08
  • 2
    Best part of the nice long linked answer is the last line: `It would be better, however, to be more specific about what you want to accomplish, so that the answer can be more to the point.` – som-snytt Mar 09 '14 at 06:00
  • @TravisBrown I think this is useful mainly for debugging. Once I was playing with an implicit function converting a String to Int, and I ended up with a Java null-pointer exception for an Int. Turned out the input for my implicit was a `null: String`, which I forgot to convert to 0. In such cases it's useful to be able to print the type of you `null`. :-) – SzG Jul 01 '17 at 09:30

1 Answers1

18

I believe you can just use:

c.getClass
The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75
user3396298
  • 204
  • 2
  • 3