Defining a type in a recursive way in Dotty

I’ve found a weird workaround: https://scastie.scala-lang.org/AWO8pKQkSUSPgWmgSBp4gg

type Rec[F[_], A] = A match {
  case Int => Int | F[Rec[F, Int]]
  case _ => A | F[Rec[F, A]]
}

This is actually equivalent to

// illegal cyclic reference
type Rec[F[_], A] = A | F[Rec[F, A]] 

But for some reason once you put it in a match type with at least 1 concrete case dotty suddenly stops complaining.

I guess there has to be a bug here somewhere. I think either both or neither should compile.

By the way you can also leave out F and only do List. That doesn’t change anything.

2 Likes