50min (5min student activity)
let students discuss among themselves about the meaning of the code without the types
# <span class="fa-stack"><i class="fa-solid fa-circle fa-stack-2x"></i><i class="fa-solid fa-book fa-stack-1x fa-inverse"></i></span> Casting - We can convert any static error into a dynamic one: casting turns compile-time errors into runtime errors <div class="grid grid-cols-2 gap-4"> <div> **Java** ```java int a = 5; a = (int)(Object)"hello"; ``` </div> <div> **Scala** ```scala var a = 5 a = "hello".asInstanceOf[Int] ``` </div> </div> * Compiler accepts code, but invalid cast is detected at runtime ```sh ClassCastException: class String cannot be cast to class Integer ``` ---
# <span class="fa-stack"><i class="fa-solid fa-circle fa-stack-2x"></i><i class="fa-solid fa-book fa-stack-1x fa-inverse"></i></span> Type Inference - Type inference infers the most precise type possible <div class="grid grid-cols-2 gap-4"> <div> **Java** ```java var a = 5; a = "hello"; ``` </div> <div> **Scala** ```scala var a = 5 a = "hello" ``` </div> </div> ```sh error: incompatible types: String cannot be converted to int a = "hello"; ^ ``` ---