# Defining a type in a recursive way in Dotty

**URL:** <https://users.scala-lang.org/t/defining-a-type-in-a-recursive-way-in-dotty/6798>\
**Category:** Question\
**Created:** [October 31, 2020, 8:09am UTC](https://users.scala-lang.org/t/defining-a-type-in-a-recursive-way-in-dotty/6798 "2020-10-31T08:09:26Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashwinbhaskar](https://users.scala-lang.org/user_avatar/users.scala-lang.org/ashwinbhaskar/32/867_2.png) [@ashwinbhaskar](https://users.scala-lang.org/u/ashwinbhaskar)\
**Post date:** [October 31, 2020, 8:09am UTC](https://users.scala-lang.org/t/defining-a-type-in-a-recursive-way-in-dotty/6798/1 "2020-10-31T08:09:26Z")

</div>

I want my List’s type parameters to be a mixture of a `String`, `List[String]`, `List[List[String]]`…and so on.

With dotty’s union types I could defined `List[String | List[String] | List[List[String]]...]`. But then I will not be able to represent up to an infinite depth.

The following code gives `illegal cyclic reference error`

```auto
type Elem = String | List[Elem] //illegal cyclic reference error

def foo: List[Elem] = ???

```

Is it possible to represent such a type using the type system?

**Edit**  
  
Examples of the list :

```auto
List("foo")
List("foo", List("foo", "baz"), "quux", List("qewrty", List("azerty")))
List(List(List(List("foo"))))

```

---

<div class="post-metadata">

**Author:** ![qqupp](https://users.scala-lang.org/letter_avatar_proxy/v4/letter/q/59ef9b/32.png) [@qqupp](https://users.scala-lang.org/u/qqupp)\
**Post date:** [October 31, 2020, 12:12pm UTC](https://users.scala-lang.org/t/defining-a-type-in-a-recursive-way-in-dotty/6798/2 "2020-10-31T12:12:16Z")

</div>

In scala 2 you can define something like

```auto
  sealed trait Elem
  final case class S(s:String) extends Elem
  final case class L(l: List[Elem]) extends Elem

```

---

<div class="post-metadata">

**Author:** ![ashwinbhaskar](https://users.scala-lang.org/user_avatar/users.scala-lang.org/ashwinbhaskar/32/867_2.png) [@ashwinbhaskar](https://users.scala-lang.org/u/ashwinbhaskar)\
**Post date:** [October 31, 2020, 12:33pm UTC](https://users.scala-lang.org/t/defining-a-type-in-a-recursive-way-in-dotty/6798/3 "2020-10-31T12:33:46Z")

</div>

true, you can do something similar using `Enums` in dotty as well. But I wanted to know if this can be directly using types.

---

<div class="post-metadata">

**Author:** ![BalmungSan](https://users.scala-lang.org/user_avatar/users.scala-lang.org/balmungsan/32/1700_2.png) [@BalmungSan](https://users.scala-lang.org/u/BalmungSan)\
**Post date:** [October 31, 2020, 1:17pm UTC](https://users.scala-lang.org/t/defining-a-type-in-a-recursive-way-in-dotty/6798/4 "2020-10-31T13:17:17Z")

</div>

An **ADT** defines types…

But I get your point, maybe it works using a type lambda?

However, note that union-types do not work to overcome erasure.  
For example `List[Int] | List[String]` would erase to just `List` so it won’t work, for this kind of things an **ADT** is the best solution.

---

<div class="post-metadata">

**Author:** ![sangamon](https://users.scala-lang.org/user_avatar/users.scala-lang.org/sangamon/32/68_2.png) [@sangamon](https://users.scala-lang.org/u/sangamon)\
**Post date:** [October 31, 2020, 2:18pm UTC](https://users.scala-lang.org/t/defining-a-type-in-a-recursive-way-in-dotty/6798/5 "2020-10-31T14:18:17Z")

</div>

Just an idea, haven’t thought it through, may be nonsense… If this kind of data structure is central to your project(s), you might consider re-imagining `List` for this specific structure, something like (Scala 2):

```auto
sealed trait Nested[+T]
case object NNil extends Nested[Nothing]
case class Base[+T](value: T, link: Nested[T]) extends Nested[T]
case class Rec[+T](nested: Nested[T], link: Nested[T]) extends Nested[T]

```

This way you could avoid having both a `List` cons and an ADT instance per node, and it’d probably be easier to implement functionality dor traversal, folding, etc. on top. But this certainly would be considerable initial overhead and, again, I’m not really sure this makes sense at all…

---

<div class="post-metadata">

**Author:** ![jducoeur](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jducoeur/32/462_2.png) [@jducoeur](https://users.scala-lang.org/u/jducoeur)\
**Post date:** [October 31, 2020, 3:07pm UTC](https://users.scala-lang.org/t/defining-a-type-in-a-recursive-way-in-dotty/6798/6 "2020-10-31T15:07:45Z")

</div>

It wouldn’t surprise me if the answer is simple “no” – this sounds a bit problematic. But the one way I can think of it _maybe_ working is with [Match Types](http://dotty.epfl.ch/docs/reference/new-types/match-types.html), which allow you to do non-trivial computation (including recursive computation) at compile time.

That said: is the shape of the objects known at compile time? That is, do you know at compile time that this specific `val` is a `List[List[List[String]]]`, as opposed to merely a `List[String]`? If you only know that at runtime (which is more common for questions like this), then I believe the answer is a hard “no”, since Types are about compile-time knowledge, not runtime knowledge…

---

<div class="post-metadata">

**Author:** ![ashwinbhaskar](https://users.scala-lang.org/user_avatar/users.scala-lang.org/ashwinbhaskar/32/867_2.png) [@ashwinbhaskar](https://users.scala-lang.org/u/ashwinbhaskar)\
**Post date:** [November 1, 2020, 9:14am UTC](https://users.scala-lang.org/t/defining-a-type-in-a-recursive-way-in-dotty/6798/7 "2020-11-01T09:14:29Z")

</div>

No, at compile time we do not know the type.

> types are about compile-time knowledge, not runtime knowledge…

But then we are able to define it using and ADT.

---

<div class="post-metadata">

**Author:** ![Jasper-M](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jasper-m/32/872_2.png) [@Jasper-M](https://users.scala-lang.org/u/Jasper-M)\
**Post date:** [November 1, 2020, 11:49am UTC](https://users.scala-lang.org/t/defining-a-type-in-a-recursive-way-in-dotty/6798/8 "2020-11-01T11:49:26Z")

</div>

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

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

```

This is actually equivalent to

```auto
// 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.

---

<div class="post-metadata">

**Author:** ![ashwinbhaskar](https://users.scala-lang.org/user_avatar/users.scala-lang.org/ashwinbhaskar/32/867_2.png) [@ashwinbhaskar](https://users.scala-lang.org/u/ashwinbhaskar)\
**Post date:** [November 1, 2020, 2:22pm UTC](https://users.scala-lang.org/t/defining-a-type-in-a-recursive-way-in-dotty/6798/9 "2020-11-01T14:22:48Z")

</div>

Wow, very interesting. In REPL, I get this

```auto
type Rec[F[_], A] = A match {
     | case String => String | F[Rec[F, String]]
     | case _ => A | F[Rec[F, A]]
     | }

scala> val a: Rec[List, String] = List("foo", List("goo", "zoo"), "baz")
val a: String | List[LazyRef(Rec[List, String])] = List(foo, List(goo, zoo), baz)

```

Notice the `LazyRef` in the type of `a`. I think that is what allows it to compile.

---

<div class="post-metadata">

**Author:** ![martijnhoekstra](https://users.scala-lang.org/user_avatar/users.scala-lang.org/martijnhoekstra/32/424_2.png) [@martijnhoekstra](https://users.scala-lang.org/u/martijnhoekstra)\
**Post date:** [November 1, 2020, 3:53pm UTC](https://users.scala-lang.org/t/defining-a-type-in-a-recursive-way-in-dotty/6798/10 "2020-11-01T15:53:33Z")

</div>

Does the puzzlers maintainer take scala3 puzzlers yet?

More seriously, maybe that warrants a bug report.

---

<div class="post-metadata">

**Author:** ![Jasper-M](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jasper-m/32/872_2.png) [@Jasper-M](https://users.scala-lang.org/u/Jasper-M)\
**Post date:** [November 1, 2020, 4:04pm UTC](https://users.scala-lang.org/t/defining-a-type-in-a-recursive-way-in-dotty/6798/11 "2020-11-01T16:04:00Z")

</div>

Agreed [https://github.com/lampepfl/dotty/issues/10136](https://github.com/lampepfl/dotty/issues/10136)
