Scala - Matching on Type Code Example

The code for Matching on Type

val values = List(123, "Hello", 123.45, (10,20,30))

for (value <- values) {
 value match {
 case i: Int => println("It is an integer: " + i)
 case s: String => println("It is a String: " + s)
 case f: Double => println("It is a Double: " + f)
 case other => println("Something else: " + other)
 }
}


/*
Output:
It is an integer: 123
It is a String: Hello
It is a Double: 123.45
Something else: (10,20,30)
*/
Code by IncludeHelp, on August 7, 2022 17:53

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.