Union Types

In this free Scala training video, I demonstrate Union Types in Scala 3.

Union types add something of a dynamic programming feature to Scala, which is a statically-typed programming language. For instance, you can create a function like this:

def isTrue(a: Int | String | Boolean): Boolean = ???

which let you specify that you want to allow isTrue to take an Int, String, or Boolean type as an input parameter. In Scala 2, the only way you could do this was to specify that you want to take the Any type in your function, and then pattern-match on the desired types inside the function. But in Scala 3 you can use this more-direct approach, which feels somewhat dynamic and flexible, and also serves to document your function more accurately in regard to the types that are allowed.

In addition to showing how to use union types for function input parameters, I also show how to use union types for the function return type, and in other situations.