Dotty – Union Data Types

Reading Time: 2 minutes

Hello folks, As we know that dotty is new Scala compiler (also know as Scala 3.0) which is coming with some new features and improvements. To get more details about the dotty and its environment setup. Please follow our beginner guide blog.

In this blog, I will describe you about newly introduced Union data type of dotty and its various properties.

A union is data type, which is denoted by “|” pipe infix operator and it looks like A | B. A union type (A | B) has a values of all values of type A and also all values of type B.

The primary reason for introducing union types in Scala is that they allow us to guarantee that for every set of types, we can always form a finite least upper bound (lub). This is both useful in practice as well as in theory. The type system of Scala 3 (i.e. dotty) is based on the DOT calculus, which has also a union data types.

A union type can also be joined with a non union types and form the finite join types. Like we can define join of a union type T1 | … | Tn as the smallest intersection type of base class instances of T1,…,Tn.

Syntactically, unions follow the same rules as intersections, but have a lower precedence than intersection(&).

Example of Dotty’s Union Types –

You can find the complete example of Dotty Union Type from here.

In this example, we have two case class models:

  • UserName
  • Password

Both have a common method lookup(). Which display the UserName and Password info on basis of its type calling.

We have also defined a getLoginInfo() method which takes a union type as an argument and return a union type value.

def getLoginInfo(loginInfo: UserName | Password): Password | UserName = {
    loginInfo match {
      case u@UserName(_) =>
        u.lookup
        u
      case p@Password(_) =>
        p.lookup
        p
    }
  }

Union types holds the commutative properties example: A | B =:= B | A

 val value1: UserName | Password = getLoginInfo(UserName("Narayan"))
  val value2: Password | UserName = getLoginInfo(UserName("Narayan"))
  if(value1 == value2) println("Commutative properties satisfy") else 
            println("Commutative properties does not satisfy")

Other Important points are –

  • Union type also supports distributive properties over the intersection (&) type.
  • Join operation on union types guarantees that the join is always finite.
  • The members of a union type are also the members of its join.
  • Union type “|” operator has lower precedence than both intersection(&) and type inference pattern (“:”) operators.
  • We can effectively use union type in pattern matching.

In next blog we will describe few more new data types of dotty. Like Type lambdas, Match types etc.

Stay Tunes, happy learning  !!!

References:

https://dotty.epfl.ch/docs/reference/new-types/union-types.html

Knoldus-blog-footer-image

Written by 

Narayan Kumar is a Sr. Software Consultant having experience of more than 3.5 years. He is passionate about Scala development and have worked on the complete range of Scala Ecosystem. He is a quick learner & curious to learn new technologies. He is responsible and a good team player. He has a good understanding of building streaming application on Apache Spark, Kafka and Cassandra.