Opaque Types

In this free Scala training video I discuss Opaque Types in Scala 3.

Opaque types were introduced in Scala 3, and they let you create type aliases that restrict visibility to the underlying representation. This means that they provide a way to define types with concrete implementations, while hiding the implementation details from the users of the type. This is a technique that’s used in Domain-Driven Design as well as Functional Programming.

In this video I show how you might create ProductId and CustomerId types as opaque types, and they can be backed by the Scala Int (or Long, BigInt, or other built-in data types). Inside your opaque type definition you can see that the backing type is an Int, but programmers outside that definition work with these new ProductId and CustomerId types that you create (never seeing the Int).