But sometimes it's a bit tedios. For example
Object a = new String("tada");
Object result = null;
if(a instanceOf Number){
result = "its a number";
}
else if(a instanceOf String){
result = "its a string";
}
System.out.println(result);
ok it's a silly example, now the pretty scala approach using match
var a:Any = "tada..."
val result = a match {
case n:Number => "it's a number"
case s:String => "it's a string"
}
println(result)
oh so pretty and easy to read.
No comments:
Post a Comment