# Transparent term aliases

**URL:** <https://contributors.scala-lang.org/t/transparent-term-aliases/5553>\
**Category:** Language Design\
**Created:** [January 28, 2022, 1:27pm UTC](https://contributors.scala-lang.org/t/transparent-term-aliases/5553 "2022-01-28T13:27:31Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![pavelfatin](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/pavelfatin/32/2342_2.png) [@pavelfatin](https://contributors.scala-lang.org/u/pavelfatin)\
**Post date:** [January 28, 2022, 1:27pm UTC](https://contributors.scala-lang.org/t/transparent-term-aliases/5553/1 "2022-01-28T13:27:31Z")

</div>

To make `scala.collection.immutable.List` accessible without `import`, `package object scala` defines both a type alias and “term alias”:

```scala
type List[+A] = scala.collection.immutable.List[A] // Transparent
val List = scala.collection.immutable.List // Opaque

```

This is a commonly used technique – in `scala`, `scala.Predef`, and in many libraries. So far so good. However, the former alias is de jure, whereas the latter - only de facto. The `type` is transparent, the `val` is opaque - you shouldn’t look at the right-hand side, and even if you do, there’s no (formal) guarantee that it will stay the same.

This is not a problem for compiling and running the code, but is a problem for editing the code - how an IDE would know that the `List` term is actually `scala.collection.immutable.List`? And vice versa: if you look for usages of `scala.collection.immutable.List`, you probably also want to see usages of `List`. We may add heuristics, but that’s hardly elegant.

While we now have both transparent and opaque type alias members (in Scala 3), there’s no way to define a transparent term alias. Although `import`s (inlcuding ones with renaming) are transparent (if you navigate to an imported name, you navigate to the defintion, not to the `import` statement), they are not members, and so are not “exportable” outside the scope:

```auto
import scala.collection.immutable.List // Not exported

```

`export`s are “exportable”, but they are not transparent (?):

```scala
package object scala {
  export scala.collection.immutable.List // Opaque
}

```

Interestingly, a `val` is effectively transparent if it’s a [constant value definition](https://scala-lang.org/files/archive/spec/2.13/04-basic-declarations-and-definitions.html#value-declarations-and-definitions) – a `final val` without a type annotation and with a [constant expression](https://www.scala-lang.org/files/archive/spec/2.13/06-expressions.html#constant-expressions) as the RHS. However, constant expressions don’t seem to include `object`s (?), which is strange, given how this works in the [literal-based singleton types](https://docs.scala-lang.org/sips/42.type.html). Even though `object` is effectively `lazy val`, it’s transparent, because the RHS is synthesized by the compiler.

Can, in principle, the language treat a reference to an `object` as a constant expression (at least for top-evel `object`s), and the standard library make `scala.List`, as well as other wanna-be-transparent term aliases, `final`? Then Scala code can have transparent term aliases, which can be correctly handled by IDEs. (The key here is the semantics; whether the compiler replaces LHS with RHS in the bytecode, as it does with literals, is an implementation detail.)

---

<div class="post-metadata">

**Author:** ![prolativ](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/prolativ/32/2133_2.png) [@prolativ](https://contributors.scala-lang.org/u/prolativ)\
**Post date:** [January 28, 2022, 1:46pm UTC](https://contributors.scala-lang.org/t/transparent-term-aliases/5553/2 "2022-01-28T13:46:40Z")

</div>

Wouldn’t

```scala
inline def List = scala.collection.immutable.List

```

work as a transparent term alias? @nicolasstucki

---

<div class="post-metadata">

**Author:** ![pavelfatin](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/pavelfatin/32/2342_2.png) [@pavelfatin](https://contributors.scala-lang.org/u/pavelfatin)\
**Post date:** [January 28, 2022, 1:52pm UTC](https://contributors.scala-lang.org/t/transparent-term-aliases/5553/3 "2022-01-28T13:52:09Z")

</div>

Ideally, it would be great to also support Scala 2 (especially given that Scala 3 reuses the standard library).

---

<div class="post-metadata">

**Author:** ![soronpo](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/soronpo/32/533_2.png) [@soronpo](https://contributors.scala-lang.org/u/soronpo)\
**Post date:** [January 28, 2022, 2:18pm UTC](https://contributors.scala-lang.org/t/transparent-term-aliases/5553/4 "2022-01-28T14:18:17Z")

</div>

Wouldn’t this achieve what you want?

```scala
final val List = scala.collection.immutable.List

```

---

<div class="post-metadata">

**Author:** ![pavelfatin](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/pavelfatin/32/2342_2.png) [@pavelfatin](https://contributors.scala-lang.org/u/pavelfatin)\
**Post date:** [January 28, 2022, 2:27pm UTC](https://contributors.scala-lang.org/t/transparent-term-aliases/5553/5 "2022-01-28T14:27:35Z")

</div>

Yes, that’s one part of it. However, the language also needs to treat `scala.collection.immutable.List` as a _constant expression_, because that’s required for the `val` to be a _constant value definition_, and currently this doesn’t seem to be the case (?). We don’t even need to change anything in the compiler, only for the specification to explicitly say so 🙂

But, in practice, just adding the `final` modifiers may be already sufficient - an IDE can rely on that to detect the intent.

---

<div class="post-metadata">

**Author:** ![nicolasstucki](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/nicolasstucki/32/2790_2.png) [@nicolasstucki](https://contributors.scala-lang.org/u/nicolasstucki)\
**Post date:** [January 28, 2022, 2:54pm UTC](https://contributors.scala-lang.org/t/transparent-term-aliases/5553/6 "2022-01-28T14:54:09Z")

</div>

Maybe it should be

```auto
inline def List: scala.collection.immutable.List.type = scala.collection.immutable.List

```

to make sure the type is not widen

---

<div class="post-metadata">

**Author:** ![Primetalk](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/primetalk/32/2324_2.png) [@Primetalk](https://contributors.scala-lang.org/u/Primetalk)\
**Post date:** [January 28, 2022, 4:53pm UTC](https://contributors.scala-lang.org/t/transparent-term-aliases/5553/7 "2022-01-28T16:53:27Z")

</div>

```scala
transparent inline def List = ...

```

could be enough.

---

<div class="post-metadata">

**Author:** ![pavelfatin](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/pavelfatin/32/2342_2.png) [@pavelfatin](https://contributors.scala-lang.org/u/pavelfatin)\
**Post date:** [January 28, 2022, 8:18pm UTC](https://contributors.scala-lang.org/t/transparent-term-aliases/5553/8 "2022-01-28T20:18:08Z")

</div>

`inline` / `transparent inline` seems to fit, thanks! So transparent term aliases should be possible in Scala 3.

(As for Scala 2 and the standard library – we may still at least make those `val`s `final`.)

---

<div class="post-metadata">

**Author:** ![Jasper-M](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/jasper-m/32/1192_2.png) [@Jasper-M](https://contributors.scala-lang.org/u/Jasper-M)\
**Post date:** [January 29, 2022, 9:50am UTC](https://contributors.scala-lang.org/t/transparent-term-aliases/5553/9 "2022-01-29T09:50:12Z")

</div>

IIRC the only thing that’s special about a constant value definition is that the val will receive a singleton type instead of a widened type (since there is syntax for singleton types for primitives and strings the concept is a bit redundant). But in `val List = scala.collection.immutable.List` the val will have a singleton type either way. So it’s not clear to me what would have to change or why.

> [@pavelfatin](#):
>
> there’s no (formal) guarantee that it will stay the same.

There is a formal guarantee. Its type is `s.c.i.List.type`. There’s only one possible value. You don’t have to inspect the RHS to know that.

---

<div class="post-metadata">

**Author:** ![pavelfatin](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/pavelfatin/32/2342_2.png) [@pavelfatin](https://contributors.scala-lang.org/u/pavelfatin)\
**Post date:** [January 29, 2022, 12:10pm UTC](https://contributors.scala-lang.org/t/transparent-term-aliases/5553/10 "2022-01-29T12:10:16Z")

</div>

Good point. However, there’s more than one possible value, because [singleton types](https://www.scala-lang.org/files/archive/spec/2.13/03-types.html#singleton-types) also include `null` (at least in Scala 2):

```scala
val List: scala.collection.immutable.List.type = null

```

Besides, there could be no value:

```scala
val List: scala.collection.immutable.List.type = ???

```

So we cannot think of `List` as a _compile-time constant_ just because of the type. Technically, there should be an additional, formal guarantee for that. (In practice, however, we may consider the presence of a singleton type good enough.)

From [Inline | Macros in Scala 3 | Scala Documentation](https://docs.scala-lang.org/scala3/guides/macros/inline.html#inline-constants) it seems that literal-based singleton types have been an indirect means to enable inlining (which doesn’t include `object`s) :

> The `final` modifier will ensure that `pi` and `pie` will take a literal type. Then the constant propagation optimization in the compiler can perform inlining for such definitions. However, this form of constant propagation is best-effort and not guaranteed. Scala 3.0 also supports `final val`-inlining as best-effort inlining for migration purposes.

Scala 3 now explicitly uses [inline](https://dotty.epfl.ch/docs/reference/metaprogramming/inline.html) rather than `final` to define constant values:

> The `Config` object contains a definition of the `inline` value `logging`. This means that `logging` is treated as a _constant value_, equivalent to its right-hand side `false`. The right-hand side of such an `inline val` must itself be a [constant expression](https://scala-lang.org/files/archive/spec/2.13/06-expressions.html#constant-expressions).

There’s an explicit restriction that “only constant expressions may appear on the right-hand side of an inline value definition”:

```scala
inline val List = scala.collection.immutable.List
// Error: inline value must contain a literal constant value.

```

---

<div class="post-metadata">

**Author:** ![pavelfatin](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/pavelfatin/32/2342_2.png) [@pavelfatin](https://contributors.scala-lang.org/u/pavelfatin)\
**Post date:** [January 29, 2022, 12:37pm UTC](https://contributors.scala-lang.org/t/transparent-term-aliases/5553/11 "2022-01-29T12:37:34Z")

</div>

In Scala 2, it’s possible to write:

```scala
final val List = scala.collection.immutable.List

```

It’s not formally a constant value definition, but at least there’s a clear intent.

To make this a compile-time constant, the specification should say that a reference to `object` is a constant expression (regardless of whether the compiler actually inlines it in the bytecode). Then we would have a formal way to define transparent term aliases, even in Scala 2.

---

<div class="post-metadata">

**Author:** ![pavelfatin](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/pavelfatin/32/2342_2.png) [@pavelfatin](https://contributors.scala-lang.org/u/pavelfatin)\
**Post date:** [January 29, 2022, 2:53pm UTC](https://contributors.scala-lang.org/t/transparent-term-aliases/5553/12 "2022-01-29T14:53:07Z")

</div>

To see why this matters in practice, consider that, after the collections redesign in 2.13, most references to collections (`Traversable`, `Iterable`, `Seq`, `IndexedSeq`, `Iterator`, `List`, `Map`, etc.) in Scala code are actually references to aliases (and this idiom has been adopted by other libraries).

This scheme was supposed to be an implementation detail, and to work in the same way as the implicit imports (`scala._`, `scala.Predef._`). However, now the navigation takes you to aliases rather than classes, the action to show documentation shows nothing useful, finding usages is problematic, even the syntax highlighting is different because of the aliases. All this obviously affects the UX.

To improve the UX in IDEs, ideally, we want:

- a formal way to define a term alias (in contrast to implementing an ad hoc heuristic),
- a way to express the clear intent (in contrast to, say, treating any accidental singleton type as an alias; just as treating any `implicit` `Set[String]` as an implicit conversion from `Int` to `String`),
- something that is applicable to Scala 2 (because Scala 2 is still widely used, and the standard library is reused in Scala 3),
- an approach that is future-proof (e.g. if we are supposed to use `export` for that in Scala 3, maybe it should be possible to add `transparent`).

---

<div class="post-metadata">

**Author:** ![soronpo](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/soronpo/32/533_2.png) [@soronpo](https://contributors.scala-lang.org/u/soronpo)\
**Post date:** [January 31, 2022, 1:15am UTC](https://contributors.scala-lang.org/t/transparent-term-aliases/5553/13 "2022-01-31T01:15:26Z")

</div>

If the main problem is documentation and IDEs, maybe we should just focus on that. Ability to “export documentation” via annotations for scala-doc may be enough.

---

<div class="post-metadata">

**Author:** ![pavelfatin](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/pavelfatin/32/2342_2.png) [@pavelfatin](https://contributors.scala-lang.org/u/pavelfatin)\
**Post date:** [January 31, 2022, 8:37am UTC](https://contributors.scala-lang.org/t/transparent-term-aliases/5553/14 "2022-01-31T08:37:04Z")

</div>

It’s not just documentation – here’s a list for the IntelliJ Scala plugin (so far):

- [SCL-19928 Heuristics for transparent term aliases](https://youtrack.jetbrains.com/issue/SCL-19928)
- [SCL-19929 An option to navigate to an alias target directly](https://youtrack.jetbrains.com/issue/SCL-19929)
- [SCL-19930 Quick Documentation / Definition: handle transparent aliases](https://youtrack.jetbrains.com/issue/SCL-19930)
- [SCL-19932 Find Usages: support type and term aliases (in both directions)](https://youtrack.jetbrains.com/issue/SCL-19932)
- [SCL-19933 Highlight aliases in the same way as targets](https://youtrack.jetbrains.com/issue/SCL-19933)
- [SCL-19934 Optimize import: take aliases into account](https://youtrack.jetbrains.com/issue/SCL-19934)
- [SCL-19935 Read the RHS of constant val definitions (from .class & .tasty)](https://youtrack.jetbrains.com/issue/SCL-19935)

All this stems from the semantics – the pattern with two aliases was supposed to be just another way to import things by default, but it’s very different from `import` as far as IDE is concerned (e.g. if you navigate to an `import`ed name, you navigate to the definition, not to the `import` statement). If we had some standard way to define a “transparent export”, that would fix these issues automagically.

Of course we can (and will) try to work around this at the IDE side, but it would be great to also address that at the language side, if possible.
