Please log in to watch this conference skillscast.
In object oriented languages one can compare instances of objects either by their identity or by inspecting their internal structure and looking at the values of their members. The Scala compiler can analogically compare types using two different strategies called nominal and structural typing respectively.
Even though nominal typing is used most of the time to guarantee type safety of Scala programs there are situations when it might be too constraining and decrease developers’ efficiency. In some cases this might even lead programmers to falling back to manual type casts or accessing members of a data type by their labels using runtime strings even though they’re known at compile time. Luckily for Scala coders trapped between Scylla and Charybdis, structural typing comes to the rescue as a decent compromise between these two extremes.
I will open my talk by showing the differences between nominal and structural typing and examining their relation to duck typing in the context of a statically typed language like Scala. I will also explain the role of type refinements in Scala’s type system.
Next, I will introduce Scala 3’s Selectable trait and its older sibling Dynamic. I will delineate how they’re handled by the compiler and how they can be composed with other features of Scala, including its new metaprogramming API.
Concludingly, I will demonstrate some real world use cases where the mentioned mechanisms can be used to make your code concise and expressive, yet type-safe. I will also mention some situations where you should hold your horses and consider some alternative solutions.
YOU MAY ALSO LIKE: