What is TypeA with TypeB

I’m reading a blog post: http://milessabin.com/blog/2011/06/09/scala-union-types-curry-howard/

The author mentions a syntax (A with B) which I’m not familiar with. Is this something the author is introducing in the blog post, or is it a feature already provided by Scala, or perhaps Scalaz? The author intends it to be an intersection type.

I’m familiar already with some other languages which support union and intersection types, and I’m trying to understand what Scala supports and does not.

The A with B syntax is standard Scala syntax for intersection types. Note that with is not commutative, so A with B is not the same type as B with A. (this will be replaced in Scala 3)

Union types are not supported directly in Scala 2.x, but libraries like Scalaz provide them (in a similar way like in the blog post) and Scala 3 will also have them.

A with B exists in Scala. Mostly, it is used after extends, but it can be used in other contexts, too

Welcome to Scala 2.12.4 (OpenJDK 64-Bit Server VM, Java 1.8.0_181).
Type in expressions for evaluation. Or try :help.

**scala> trait A; trait B
defined trait A
defined trait B

class C extends A with B
defined class C

val ab: A with B = new C
ab: A with B = C@4a9860

Thanks for the explanation. So I’m not sure what it means for an intersection to not be commutative? If its not commutative, then it’s not really an intersection. Right?

What are the elements of the type A with B, and what are the instances of B with A?

Or does the concept even exist in Scala: “elements of a type” ?

I’m not the expert, but I believe that’s correct. Note that this is changing in Scala 3, which is replacing the old with types with true intersection types and union types.

1 Like

This is due to the inherent problem of multiple inheritance: it tries to solve the
issue of inheriting common base classes.

See for example:


It is commutative when used purely as a type. When used in an extends clause, you can get different implementations.

Every object that is of type A with B is also of type B with A and vice versa, hence A with B is the same type as B with A.

If, however, both A and B define the same member, then in an extends clause the last one wins. This kind of implementation resolution is known as linearization.

**Welcome to Scala 2.12.4 (OpenJDK 64-Bit Server VM, Java 1.8.0_181).
Type in expressions for evaluation. Or try :help.

trait A { def greet: Unit = println(“Hello!”) }
defined trait A

trait B { def greet: Unit = println(“Yo!”) }
defined trait B

class C extends A with B { override def greet: Unit = super.greet }
defined class C

class D extends B with A { override def greet: Unit = super.greet }
defined class D

val c = new C
c: C = C@24e5389c

val d = new D
d: D = D@6d17914a

c.greet
Yo!

d.greet
Hello!

c.isInstanceOf[A with B]
res2: Boolean = true

c.isInstanceOf[B with A]
res3: Boolean = true

d.isInstanceOf[A with B]
res4: Boolean = true

d.isInstanceOf[B with A]
res5: Boolean = true**

That Scala 3 will support true intersection and union types (but apparently not complementary types) is very interesting. Has this been presented at as a conference or journal paper? I’d love to use this citation in my thesis.

For example, I found this talk by Odersky, https://www.youtube.com/watch?v=9lWrt6H6UdE&frags=pl%2Cwn
But certainly he or some of his students have published the ideas.

I found the following two which should be more than enough for me. http://lampwww.epfl.ch/~amin/drafts/dot_oopsla16.pdf
http://lampwww.epfl.ch/~amin/dot/fool.pdf