# Proposal for Multi-level Enums

**URL:** <https://contributors.scala-lang.org/t/proposal-for-multi-level-enums/3344>\
**Category:** Language Design\
**Created:** [May 26, 2019, 5:01pm UTC](https://contributors.scala-lang.org/t/proposal-for-multi-level-enums/3344 "2019-05-26T17:01:38Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![joshlemer](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/joshlemer/32/39_2.png) [@joshlemer](https://contributors.scala-lang.org/u/joshlemer)\
**Post date:** [May 26, 2019, 5:01pm UTC](https://contributors.scala-lang.org/t/proposal-for-multi-level-enums/3344/1 "2019-05-26T17:01:38Z")

</div>

Dotty has added a really fabulous way of declaring Algebraic Data Types (ADTs) in the form of [the new `enum` syntax](http://dotty.epfl.ch/docs/reference/enums/enums.html)

e.g.

```scala
enum MyEnum {
  case A
  case B(i: Int, j: Int)
}

```

However these ADT are limited in that they can only be completely flat, while in general it is actually quite useful oftentimes to nest ADTs, and the new enum syntax seems like a great opportunity to provide support for those! I would propose the following syntax

```scala
enum MyEnum {
  case A
  case B(i: Int)
  case enum C {
    case D(s: String)
    case E
    case enum F {
      case G
    }
    case enum H {
      case I, J, K
    }
  }
}

```

But then the question arises: What type should be inferred for nested enum values, such as `MyEnum.A`, `MyEnum.B.apply`, and `MyEnum.C.D.apply(s: String)`?

I think a simple approach would be: Always infer the most specific enclosing enum. So each of the following expressions should have these types inferred

```scala
MyEnum.A // MyEnum
MyEnum.B(1) // MyEnum
MyEnum.C.D("") // MyEnum.C
MyEnum.C.E // MyEnum.C
MyEnum.C.F.G // MyEnum.C.F
MyEnum.C.H.I // MyEnum.C.H
MyEnum.C.H.J // MyEnum.C.H
MyEnum.C.H.K // MyEnum.C.H

```

Below I will include a collection of nice usecases of this feature, to demonstrate that it is not just for giggles:

1. Paul Philips’ `SizeInfo` talked about [here](https://youtu.be/uiJycy6dFSQ?t=2465)  
 ![17%20AM](https://contributors.scala-lang.org/uploads/default/original/2X/b/b72d0f38a7eeb7343a8f0beb8a5c6153c19f7929.jpeg)

```scala
enum SizeInfo {
  case Bounded(bound: Int)
  case enum Atomic {
    case Infinite
    case Precise(n: Int)
  }
}

```

1. A Hierarchy of Reals/Rationals/Integers/Naturals

```scala
enum Real(computeDigits: Int => BigDecimal) {
  case enum Rational(numerator: Int, denominator: Int) 
      extends Real(_ => BigDecimal(numerator) / denominator) {
    case enum Integer(n: Int) extends Rational(n, 1) {
      case Natural(n: Int) extends Integer(n)
      case Negative(n: Int) extends Integer(n)
    }
    case Fraction(numerator: Int, denominator: Int) extends Rational(numerator, denominator)
  }
  case Irrational(computeDigits: Int => BigDecimal) extends Real(computeDigits)
}

```

1. An generalization of `Either` to include “Inclusive OR”

```scala
enum AndOr[+A, +B] {
  case Both[+A, +B](left: A, right: B) extends AndOr[A, B]
  case enum Either[+A, +B] extends AndOr[A, B] {
    case Left[+A, +B](value: A) extends Either[A, B]
    case Right[+A, +B](value: B) extends Either[A, B]
  }
}

```

1. Grouping JSON values into primitives and non primitives

```scala
enum JsValue {
  case Obj(fields: Map[String, JsValue])
  case Arr(elems: ArraySeq[JsValue])
  case enum Primitive {
    case Str(str: String)
    case Num(bigDecimal: BigDecimal)
    case JsNull
    case enum Bool(boolean: Boolean) { 
      case True extends Bool(true), 
      case False extends Bool(false) }
  }
}

```

---

<div class="post-metadata">

**Author:** ![oscar](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/oscar/32/211_2.png) [@oscar](https://contributors.scala-lang.org/u/oscar)\
**Post date:** [May 26, 2019, 5:19pm UTC](https://contributors.scala-lang.org/t/proposal-for-multi-level-enums/3344/2 "2019-05-26T17:19:19Z")

</div>

This looks really great to me. Subclasses of ADTs can be very useful. Would be great to have a nice syntax.

---

<div class="post-metadata">

**Author:** ![nrinaudo](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/nrinaudo/32/2307_2.png) [@nrinaudo](https://contributors.scala-lang.org/u/nrinaudo)\
**Post date:** [May 26, 2019, 5:27pm UTC](https://contributors.scala-lang.org/t/proposal-for-multi-level-enums/3344/3 "2019-05-26T17:27:10Z")

</div>

I used this kind of nesting fairly often when I have complicated error ADTs. A ReadError can be DecodeError, a ParseError or an IOError. A DecodeError can be a TypeError or a NotFoundError. Etc…

---

<div class="post-metadata">

**Author:** ![LPTK](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/lptk/32/1620_2.png) [@LPTK](https://contributors.scala-lang.org/u/LPTK)\
**Post date:** [May 26, 2019, 5:27pm UTC](https://contributors.scala-lang.org/t/proposal-for-multi-level-enums/3344/4 "2019-05-26T17:27:39Z")

</div>

I second this proposal. Multi-level enums can be really useful for encoding more precise invariants into ADTs, without having to resort to nesting (which adds clutter and extra allocations).

---

<div class="post-metadata">

**Author:** ![stewSquared](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/stewsquared/32/3172_2.png) [@stewSquared](https://contributors.scala-lang.org/u/stewSquared)\
**Post date:** [May 26, 2019, 6:44pm UTC](https://contributors.scala-lang.org/t/proposal-for-multi-level-enums/3344/5 "2019-05-26T18:44:49Z")

</div>

> [@joshlemer](#):
>
> But then the question arises: What type should be inferred for nested enum values, such as `MyEnum.A` , `MyEnum.B.apply` , and `MyEnum.C.D.apply(s: String)` ?
> 
> I think a simple approach would be: Always infer the most specific enclosing enum.

Good idea. This should work for inference since nested parameterized `enum` nodes aren’t `case` classes like their siblings. We want to avoid [this type inference problem](https://contributors.scala-lang.org/t/could-adts-extend-product-with-serializable-in-dotty/3304).

That said, is the current implementation of enums necessarily flat? Since the top-level enum is `sealed` rather than `final`, couldn’t we could already do something like the following?

```scala
enum JsValue { // desugars to a sealed abstract class
  case Obj(fields: Map[String, JsValue])
  case Arr(elems: ArraySeq[JsValue])
}
enum Primitive extends JsValue {
  case Str(str: String)
  case Num(bigDecimal: BigDecimal)
  case JsNull
}
enum Bool(boolean: Boolean) extends Primitive { 
  case True extends Bool(true), 
  case False extends Bool(false)
}

```

(Granted, the nested version is an improvement that’s easier to read and keeps all the types contained under the top level enum, and `enumTag` might not be built to handle this gracefully. If we did this, `enum` should probably also imply `final`)

---

<div class="post-metadata">

**Author:** ![lihaoyi](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/lihaoyi/32/103_2.png) [@lihaoyi](https://contributors.scala-lang.org/u/lihaoyi)\
**Post date:** [May 27, 2019, 3:18am UTC](https://contributors.scala-lang.org/t/proposal-for-multi-level-enums/3344/6 "2019-05-27T03:18:50Z")

</div>

I like this and think it would be a great addition. A lot my own own ADTs are naturally hierarchical, exactly like the examples given, such as [this one](https://github.com/lihaoyi/Metascala/blob/76dfbfa18484b9ee39bd09453328ea1081fcab6b/src/main/scala/metascala/imm/Type.scala) from my Metascala JVM

```auto
sealed trait Type
object Type{
  sealed trait Ref extends Type
  object Ref{
    case class Cls extends Ref
    case class Arr extends Ref
  }
  sealed trait Prim extends Type
  object Prim{
    case object Z extends Prim
    case object B extends Prim
    case object C extends Prim
    case object S extends Prim
    case object I extends Prim
    case object J extends Prim
    case object F extends Prim
    case object D extends Prim
  }
}

```

This lets things have meaningful names like `Type.Prim.I`, while avoiding polluting the global (or even `Type.*`) namespace with dozens of prefixed `TypePrimI` names.

---

<div class="post-metadata">

**Author:** ![fanf](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/fanf/32/2730_2.png) [@fanf](https://contributors.scala-lang.org/u/fanf)\
**Post date:** [May 27, 2019, 9:36am UTC](https://contributors.scala-lang.org/t/proposal-for-multi-level-enums/3344/7 "2019-05-27T09:36:17Z")

</div>

I tried to refrain myself from doing a “me too” post… But really, not doing what is proposed here would be _huge_ missed in uniformity… I would really love to have that.

---

<div class="post-metadata">

**Author:** ![stewSquared](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/stewsquared/32/3172_2.png) [@stewSquared](https://contributors.scala-lang.org/u/stewSquared)\
**Post date:** [May 27, 2019, 4:30pm UTC](https://contributors.scala-lang.org/t/proposal-for-multi-level-enums/3344/8 "2019-05-27T16:30:33Z")

</div>

> [@fanf](#):
>
> But really, not doing what is proposed here would be _huge_ missed in uniformity… I would really love to have that.

I’d honestly be surprised if nesting has never been considered – it just seems so natural and intuitive!

---

<div class="post-metadata">

**Author:** ![nrinaudo](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/nrinaudo/32/2307_2.png) [@nrinaudo](https://contributors.scala-lang.org/u/nrinaudo)\
**Post date:** [May 27, 2019, 4:58pm UTC](https://contributors.scala-lang.org/t/proposal-for-multi-level-enums/3344/9 "2019-05-27T16:58:10Z")

</div>

Oh but it has on at least one occasion: [https://gitter.im/lampepfl/dotty?at=5cd583d2f251e60ffa5a990f](https://gitter.im/lampepfl/dotty?at=5cd583d2f251e60ffa5a990f)

---

<div class="post-metadata">

**Author:** ![joshlemer](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/joshlemer/32/39_2.png) [@joshlemer](https://contributors.scala-lang.org/u/joshlemer)\
**Post date:** [June 18, 2019, 2:32pm UTC](https://contributors.scala-lang.org/t/proposal-for-multi-level-enums/3344/10 "2019-06-18T14:32:10Z")

</div>

@smarter @nicolasstucki @odersky any thoughts? The proposal seems to be universally well received

---

<div class="post-metadata">

**Author:** ![sjrd](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sjrd/32/24_2.png) [@sjrd](https://contributors.scala-lang.org/u/sjrd)\
**Post date:** [June 18, 2019, 3:25pm UTC](https://contributors.scala-lang.org/t/proposal-for-multi-level-enums/3344/11 "2019-06-18T15:25:14Z")

</div>

My take is that we should ship what we have now in 3.0, and we can revisit this in a later version. We can extend flat enums to multilevel enums in a backward compatible way in the future, so we should give ourselves more time to weigh this, and above all to really feel the limits of flat enums, before committing to more complexity.

---

<div class="post-metadata">

**Author:** ![dwijnand](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/dwijnand/32/796_2.png) [@dwijnand](https://contributors.scala-lang.org/u/dwijnand)\
**Post date:** [June 19, 2019, 6:51am UTC](https://contributors.scala-lang.org/t/proposal-for-multi-level-enums/3344/12 "2019-06-19T06:51:53Z")

</div>

The problem with that strategy is that instead of the (preferred) “old way” and “new way” to encode ADTs you now have the “old way OR the only way to do hierarchical ADTs” and “the new, limited way”.

Is there no time to implement and test out hierarchical ADTs?

Are there no hierarchical ADTs in dotc?

---

<div class="post-metadata">

**Author:** ![nafg](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/nafg/32/38_2.png) [@nafg](https://contributors.scala-lang.org/u/nafg)\
**Post date:** [June 19, 2019, 7:33am UTC](https://contributors.scala-lang.org/t/proposal-for-multi-level-enums/3344/13 "2019-06-19T07:33:37Z")

</div>

We need to accept that languages should continually evolve, and figure out how to solve problems that brings[1], rather than keep fooling ourselves that we’re finally in possession of the Final, Complete, and Perfect design. Every bit of software that ships and we start using changes our perspective on things in ways that we can’t predict.

Sorry for being melodramatic, or if I’m over-generalizing, but you get the point…

[1] One example of a problem is “we can’t wait so long.” The solution is not to evolve in fewer iterations, but to figure out how to make each iteration much shorter.

---

<div class="post-metadata">

**Author:** ![nrinaudo](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/nrinaudo/32/2307_2.png) [@nrinaudo](https://contributors.scala-lang.org/u/nrinaudo)\
**Post date:** [June 19, 2019, 7:48am UTC](https://contributors.scala-lang.org/t/proposal-for-multi-level-enums/3344/14 "2019-06-19T07:48:24Z")

</div>

I agree wholeheartedly with that point. There are hierarchical ADTs in my code base, and that basically means I cannot (consistently) rely on enum.

That being said, we can’t port every single fancy behaviour clever devs used to encode through sealed trait hierarchies in enum. Sometimes, it’s fine to say “you can do that, but by hand”. I just don’t think hierarchical ADTs are the right cutoff, but I use them pretty often, so I might be biased.

Also, last time I checked-a few weeks ago- hierarchical ADTs compiled, but just didn’t behave as you’d expect. If we’re going to forbid them, let them at least be a compilation failure.

---

<div class="post-metadata">

**Author:** ![dwijnand](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/dwijnand/32/796_2.png) [@dwijnand](https://contributors.scala-lang.org/u/dwijnand)\
**Post date:** [February 7, 2020, 9:48am UTC](https://contributors.scala-lang.org/t/proposal-for-multi-level-enums/3344/15 "2020-02-07T09:48:22Z")

</div>

Should this be opened as an issue in [https://github.com/lampepfl/dotty/issues](https://github.com/lampepfl/dotty/issues)? (Or [https://github.com/lampepfl/dotty-feature-requests](https://github.com/lampepfl/dotty-feature-requests) - are multi-level enums a feature request or a bug report? 🥊)

---

<div class="post-metadata">

**Author:** ![jdegoes](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/jdegoes/32/1404_2.png) [@jdegoes](https://contributors.scala-lang.org/u/jdegoes)\
**Post date:** [February 7, 2020, 12:38pm UTC](https://contributors.scala-lang.org/t/proposal-for-multi-level-enums/3344/16 "2020-02-07T12:38:44Z")

</div>

Multi-level enums could be useful but could be added later, if necessary.

In the meantime, the feature can be emulated rather directly, _without_ the need to abandon enums:

e.g.:

```scala
enum MyEnum {
  case A
  case B(i: Int)
  case C(x: X)
}

final case class X(c: C)

enum C {
  case D(s: String)
  case E
}

```

Indeed, this would be the idiomatic encoding of this ADT in Haskell and other functional programming languages, which support products of sums, but not “sums of sums”, since those flatten into sums (it is only an artifact of Scala’s encoding of sums using subtyping that such a multi-level sum is even possible).

---

<div class="post-metadata">

**Author:** ![LPTK](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/lptk/32/1620_2.png) [@LPTK](https://contributors.scala-lang.org/u/LPTK)\
**Post date:** [February 7, 2020, 1:02pm UTC](https://contributors.scala-lang.org/t/proposal-for-multi-level-enums/3344/17 "2020-02-07T13:02:29Z")

</div>

Why use an intermediate `X` case class? You can have:

```scala
package foo

enum MyEnum {
  case A
  case B(i: Int)
  case C(c: foo.C)
}

enum C {
  case D(s: String)
  case E
}

```

Though this version still allocate a useless wrapper at runtime (the `MyEnum.C` case). I don’t see a reason to add a wrapper when you could just have a nested enum. But I agree that nested enums can always be added later, and so are not on the critical path.

> [@jdegoes](#):
>
> it is only an artifact of Scala’s encoding of sums using subtyping that such a multi-level sum is even possible

You could just as well say that it’s an artifact of Haskell’s _lack of subtyping_ that these are not possible in Haskell.

---

<div class="post-metadata">

**Author:** ![LPTK](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/lptk/32/1620_2.png) [@LPTK](https://contributors.scala-lang.org/u/LPTK)\
**Post date:** [February 7, 2020, 1:27pm UTC](https://contributors.scala-lang.org/t/proposal-for-multi-level-enums/3344/18 "2020-02-07T13:27:07Z")

</div>

Oh, and while we’re talking about Haskell, it’s funny to read [GHC’s source](https://haskell-code-explorer.mfix.io/package/ghc-8.6.5/show/types/TyCoRep.hs#L303) and find things like:

```auto
-- INVARIANT: The cast is never refl.

```

```auto
-- INVARIANT: The Type is not a CastTy (use TransCo instead)

```

…which could be expressed by having a more refined, multi-level ADT. I guess they don’t use wrappers there because of the added performance overhead and boilerplate these would incur.

---

<div class="post-metadata">

**Author:** ![jducoeur](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/jducoeur/32/32_2.png) [@jducoeur](https://contributors.scala-lang.org/u/jducoeur)\
**Post date:** [February 7, 2020, 2:43pm UTC](https://contributors.scala-lang.org/t/proposal-for-multi-level-enums/3344/19 "2020-02-07T14:43:58Z")

</div>

> [@jdegoes](#):
>
> Multi-level enums could be useful but could be added later, if necessary.

Agreed. I think they’d be helpful, and it’s worth a feature request, but let’s not make the best be the enemy of the good – if multi-level isn’t in scope for 3.0, we should think about how to do it in a later release.

---

<div class="post-metadata">

**Author:** ![kai](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/kai/32/1733_2.png) [@kai](https://contributors.scala-lang.org/u/kai)\
**Post date:** [February 7, 2020, 2:48pm UTC](https://contributors.scala-lang.org/t/proposal-for-multi-level-enums/3344/20 "2020-02-07T14:48:23Z")

</div>

Are enums `final`, not `sealed` ? What’s stopping me from writing

```scala
enum MyEnum {
  case A
  case B(i: Int)
}

enum C extends MyEnum {
  case D(s: String)
  case E
}

```

in the same file?

Edit: there’s a specific prohibiting error message…

```auto
class C in package extends enum MyEnum, but extending enums is prohibited.

```

But it’s possible to employ a pattern hiding the enum type for a horrible workaround:

```scala
sealed trait MyEnum
final lazy val MyEnum = MyEnum0

private enum MyEnum0 extends MyEnum {
  case A
  case B(i: Int)
}

enum C extends MyEnum {
  case D(s: String)
  case E
}

```

[Next page](https://contributors.scala-lang.org/t/proposal-for-multi-level-enums/3344.md?page=2)
