# Enum candy: the last mile

**URL:** <https://contributors.scala-lang.org/t/enum-candy-the-last-mile/6760>\
**Category:** Language Design\
**Created:** [September 14, 2024, 10:59am UTC](https://contributors.scala-lang.org/t/enum-candy-the-last-mile/6760 "2024-09-14T10:59:29Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![regiskuckaertz](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/regiskuckaertz/32/1873_2.png) [@regiskuckaertz](https://contributors.scala-lang.org/u/regiskuckaertz)\
**Post date:** [September 14, 2024, 10:59am UTC](https://contributors.scala-lang.org/t/enum-candy-the-last-mile/6760/1 "2024-09-14T10:59:29Z")

</div>

When access to term members inside an ADT is exclusively done via pattern matching or combinators, having to name members is tedious and useless. For instance:

```scala
enum Tree[+T]:
  case B(left: Tree[T], value: T, right: Tree[T])
  case E

  def fold[A](onEmpty: A, onBranch: (A, T, A) => A): A =
    def go(x: Tree[T]): TailRec[A] = x match
      case B(l, v, r) => tailcall:
        for l <- go(l)
            r <- go(r)
        yield onBranch(l, v, r)
      case E => done(onEmpty)
    
    go(this).result

```

The names `left`, `value` and `right` in `Tree.B` are never used. Would it be possible to synthesise names at compile-time and let developers write:

```scala
enum Tree[+T]:
  case B(Tree[T], T, Tree[T])
  case E

```

? We found that our datatypes fall into two categories: domain objects where names are relevant, and eDSL which are often (co-)recursive ADTs and come with interpreters/compilers, where names are irrelevant.

---

<div class="post-metadata">

**Author:** ![bishabosha](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/bishabosha/32/3101_2.png) [@bishabosha](https://contributors.scala-lang.org/u/bishabosha)\
**Post date:** [September 14, 2024, 5:35pm UTC](https://contributors.scala-lang.org/t/enum-candy-the-last-mile/6760/2 "2024-09-14T17:35:35Z")

</div>

I guess inferring `_1`, `_2` etc would be consistent - accessors with these names are already generated regardless of whichever parameter names you define

---

<div class="post-metadata">

**Author:** ![OndrejSpanel](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/ondrejspanel/32/1238_2.png) [@OndrejSpanel](https://contributors.scala-lang.org/u/OndrejSpanel)\
**Post date:** [September 16, 2024, 5:10am UTC](https://contributors.scala-lang.org/t/enum-candy-the-last-mile/6760/3 "2024-09-16T05:10:42Z")

</div>

I do not think it is worth making the syntax less regular. You can always name parameters `a`, `b`, `c` or `p1`, `p2`, `p3` if you are not interested in those names.

That said, it might be possible to have unused parameters unnamed anywhere by using underscore, like:

```auto
case B(_: Tree[T], _: T, _: Tree[T])

```

The same syntax could be used in ordinary function signatures.

---

<div class="post-metadata">

**Author:** ![regiskuckaertz](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/regiskuckaertz/32/1873_2.png) [@regiskuckaertz](https://contributors.scala-lang.org/u/regiskuckaertz)\
**Post date:** [September 16, 2024, 12:01pm UTC](https://contributors.scala-lang.org/t/enum-candy-the-last-mile/6760/4 "2024-09-16T12:01:44Z")

</div>

Only problem is the Scala community still suffers from post underscore disorder 😂 and Scala 3 did a reasonably good job at addressing it.

---

<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:** [September 16, 2024, 12:17pm UTC](https://contributors.scala-lang.org/t/enum-candy-the-last-mile/6760/5 "2024-09-16T12:17:13Z")

</div>

Granted, but this is a use case where underscore seems consistent with the its primary usage. (As I usually refer to it, underscore is the “whatever” operator, when you really don’t want a name.) Using it like this, to indicate that we are only going to care about these members positionally, makes some intuitive sense.

---

<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:** [September 16, 2024, 3:21pm UTC](https://contributors.scala-lang.org/t/enum-candy-the-last-mile/6760/6 "2024-09-16T15:21:02Z")

</div>

> [@OndrejSpanel](#):
>
> ```auto
> case B(_: Tree[T], _: T, _: Tree[T])
> 
> ```

What is the benefit of that over

```scala
case B(l: Tree[T], t: T, r: Tree[T])

```

---

<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:** [September 16, 2024, 3:21pm UTC](https://contributors.scala-lang.org/t/enum-candy-the-last-mile/6760/7 "2024-09-16T15:21:55Z")

</div>

> [@bishabosha](#):
>
> I guess inferring `_1`, `_2` etc would be consistent - they are already generated whichever parameter names you define

Why is that?

---

<div class="post-metadata">

**Author:** ![OndrejSpanel](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/ondrejspanel/32/1238_2.png) [@OndrejSpanel](https://contributors.scala-lang.org/u/OndrejSpanel)\
**Post date:** [September 16, 2024, 3:49pm UTC](https://contributors.scala-lang.org/t/enum-candy-the-last-mile/6760/8 "2024-09-16T15:49:07Z")

</div>

> [@nafg](#):
>
> What is the benefit of that over

It tries to satisfy the need in the OP specified as “having to name members is tedious and useless”.

Intent is clear, same as with function literals or pattern matchings. One could ask - what is the point of being able to write `(x, _) => x` when I could write `(x, a) => x`?

---

<div class="post-metadata">

**Author:** ![OndrejSpanel](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/ondrejspanel/32/1238_2.png) [@OndrejSpanel](https://contributors.scala-lang.org/u/OndrejSpanel)\
**Post date:** [September 17, 2024, 10:38am UTC](https://contributors.scala-lang.org/t/enum-candy-the-last-mile/6760/9 "2024-09-17T10:38:06Z")

</div>

> [@nafg](#):
>
> Why is that?

See [Old style tuple accessors on normal case classes in Scala 3 - Question - Scala Users](https://users.scala-lang.org/t/old-style-tuple-accessors-on-normal-case-classes-in-scala-3/8394)

---

<div class="post-metadata">

**Author:** ![regiskuckaertz](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/regiskuckaertz/32/1873_2.png) [@regiskuckaertz](https://contributors.scala-lang.org/u/regiskuckaertz)\
**Post date:** [September 17, 2024, 12:49pm UTC](https://contributors.scala-lang.org/t/enum-candy-the-last-mile/6760/10 "2024-09-17T12:49:13Z")

</div>

> [@OndrejSpanel](#):
>
> I do not think it is worth making the syntax less regular.

@OndrejSpanel I found myself agreeing with you on this one, but then it hit me: `enum` is a completely new construct with a few irregularities (`case` vs `case class`, no need for `extends` by default, etc.). Maybe this isn’t going _too_ far away?

There’s other corners of the language that are currently pushing the boundaries of what is acceptable syntax (lately context bounds).

---

<div class="post-metadata">

**Author:** ![som-snytt](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/som-snytt/32/595_2.png) [@som-snytt](https://contributors.scala-lang.org/u/som-snytt)\
**Post date:** [September 18, 2024, 8:41am UTC](https://contributors.scala-lang.org/t/enum-candy-the-last-mile/6760/11 "2024-09-18T08:41:12Z")

</div>

It makes the syntax more regular.

An implicit parameter is written

```auto
def f(using T)

```

and an explicit (albeit unnamed) parameter is written

```auto
def f(T)

```

which is nicer than

```auto
def f(@deprecatedName t: T) // disallow f(t = myT)

```

Instead of the weirdness that there is only one place where it matters if an identifier starts with a lowercase letter, namely in patterns, now there will be two, if a variable identifier must begin a var name, but otherwise it’s a type name.

The names are available internally as the positional names `_1` etc, thereby resolving the additional weirdness that only “varargs” look different depending on whether you’re inside the function. As the saying goes, misery loves company.

Currently, when I have a signature such as your example,

```auto
def f(t: T, otherT: T)

```

I must grab my trusty old “Tables of Names for Software Engineers” off the shelf and cross-index my type names with well-known algorithms in order to make sure all the parameters have reasonable names.

Using positional names will not only save me hours of manual labor (manual in the sense that I must consult the _ToN_ by hand), but it will spare me the agony before every code review when I have introduced a named element.

I have proposed that all code reviews be double-blind (so-called), where no one knows which variables are assigned to which computations until all the tests pass.

---

<div class="post-metadata">

**Author:** ![MateuszKowalewski](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/mateuszkowalewski/32/77_2.png) [@MateuszKowalewski](https://contributors.scala-lang.org/u/MateuszKowalewski)\
**Post date:** [September 18, 2024, 7:38pm UTC](https://contributors.scala-lang.org/t/enum-candy-the-last-mile/6760/12 "2024-09-18T19:38:05Z")

</div>

> [@som-snytt](#):
>
> ```auto
> def f(T)
> 
> ```

I always wanted something like that. But I don’t know how it could work.

In strictly typed APIs you have often quite precise types (e.g. new-type wrappers ); but you actually also want good descriptive parameter names as otherwise the code is cryptic.

Something like:

```scala
def register(name: Name, dateOfBirth: DateOfBirth, birthplace: Birthplace)

```

Very often in such cases the type names and the parameter names match, except for the case.

It would be nice to be able to write something like:

```scala
def register(Name, DateOfBirth, Birthplace)

```

But how would one than access the parameters?

By position? No, for sure not, that would lead to even more cryptic code than using `n`, `db`, `bp`, and such.

For context bounds there is this proposal to make it possible to access the given parameters by the name of the type in the context bound (though some compiler internal “virtual object”, or something like that).

But I’m not sure this could work also for method / function parameters. My gut feeling is that it would open some cans of worms from the technical side. (So I’m not proposing anything here, just thinking out loud).

But it would be somehow attractive. At least if someone had an idea how this could work.

One could also think in the opposite direction and speculate what would happen if the compiler would expand

```scala
def register(name, dateOfBirth, birthplace)

```

to the original valid definition? This could just assume types of the params to be the uppercased names.

But all that looks kind of strange, TBH. Kind of random thoughts. Is this redundancy really a problem? IDK

---

<div class="post-metadata">

**Author:** ![Zang](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/zang/32/3090_2.png) [@Zang](https://contributors.scala-lang.org/u/Zang)\
**Post date:** [September 19, 2024, 3:47am UTC](https://contributors.scala-lang.org/t/enum-candy-the-last-mile/6760/13 "2024-09-19T03:47:14Z")

</div>

Lambda can be used if parameter type can be omitted, ex:

```scala
val register = (name, dateOfBirth, birthplace) => { ... }

```

So we may not want to introduce another syntax, just relax the restriction to define a lambda.

---

<div class="post-metadata">

**Author:** ![MateuszKowalewski](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/mateuszkowalewski/32/77_2.png) [@MateuszKowalewski](https://contributors.scala-lang.org/u/MateuszKowalewski)\
**Post date:** [September 19, 2024, 4:23pm UTC](https://contributors.scala-lang.org/t/enum-candy-the-last-mile/6760/14 "2024-09-19T16:23:28Z")

</div>

How would you type such a lambda?

---

<div class="post-metadata">

**Author:** ![regiskuckaertz](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/regiskuckaertz/32/1873_2.png) [@regiskuckaertz](https://contributors.scala-lang.org/u/regiskuckaertz)\
**Post date:** [October 8, 2024, 7:07am UTC](https://contributors.scala-lang.org/t/enum-candy-the-last-mile/6760/15 "2024-10-08T07:07:49Z")

</div>

Brief survey of languages that do support the creation of algebraic data types to some extent:

- _naming not possible or optional_: haskell, f#, ocaml, elm, purescript, reasonml, rescript, clean, roc, unison, grain, gleam, flix, … rust

- _naming mandatory_: typescript, kotlin, java, python, scala

my very biased opinion is that scala would feel more at home in the top list

---

<div class="post-metadata">

**Author:** ![bishabosha](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/bishabosha/32/3101_2.png) [@bishabosha](https://contributors.scala-lang.org/u/bishabosha)\
**Post date:** [November 7, 2024, 2:44pm UTC](https://contributors.scala-lang.org/t/enum-candy-the-last-mile/6760/16 "2024-11-07T14:44:42Z")

</div>

@regiskuckaertz talked about this in [Scala.IO](http://Scala.IO) today, it seems popular 🙂
