Executing the below code
println("Pattern Matching by type")
val priceofCar : Any = 2.0
val priceType = priceofCar match{
case price: Int => "Int" // 1
case price: Float => "Float" // 2
case price: Any => "Any" // 3
case price: Double => "Double" // 4
case _ => "others" // 5
}
// println(:type priceofCar)
println(s"Car price type = $priceType")
ends up with the below output
Car price type = Any
and a warning message
warning: unreachable code
case price: Double => "Double" // 4
^
one warning found.
But, after swapping the order of rows //3 and //4, no warnings are shown and get the below output
Car price type = Double
Now my doubt is,
- either it should stick to "Double" or "Any" in both cases, but not why ?
- why the warning is thrown ?
