# Should a \`def\` with a singleton type be considered stable?

**URL:** <https://contributors.scala-lang.org/t/should-a-def-with-a-singleton-type-be-considered-stable/7075>\
**Category:** Language Design\
**Created:** [March 15, 2025, 5:22pm UTC](https://contributors.scala-lang.org/t/should-a-def-with-a-singleton-type-be-considered-stable/7075 "2025-03-15T17:22:34Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sporarum](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sporarum/32/2545_2.png) [@Sporarum](https://contributors.scala-lang.org/u/Sporarum)\
**Post date:** [March 15, 2025, 5:22pm UTC](https://contributors.scala-lang.org/t/should-a-def-with-a-singleton-type-be-considered-stable/7075/1 "2025-03-15T17:22:34Z")

</div>

`val`s make stable identifiers:

```scala
class Graph {
  class Node {
    val graph = Graph.this // val (without singleton type)
  }
  def useNode(node: Node) = ???
}

def useNode(node: Graph#Node) = node.graph.useNode(node)
// [...] Required: node.graph.Node

```

`def`s with singleton types do not (even though they will always return the same value):

```scala
class Graph {
  class Node {
    def graph: Graph.this.type = Graph.this // def with singleton type
  }
  def useNode(node: Node) = ???
}

def useNode(node: Graph#Node) = node.graph.useNode(node)
// [...] Required: [unknown type].Node

```

[/quote]

The question is then:  
Could we make it so both introduce stable identifiers ?

It seems to me like this would be as sound as a before, is there a tricky edge-case ?

---

<div class="post-metadata">

**Author:** ![JD557](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/jd557/32/3004_2.png) [@JD557](https://contributors.scala-lang.org/u/JD557)\
**Post date:** [March 16, 2025, 12:18am UTC](https://contributors.scala-lang.org/t/should-a-def-with-a-singleton-type-be-considered-stable/7075/2 "2025-03-16T00:18:22Z")

</div>

> [@Sporarum](#):
>
> even though they will always return the same value

I don’t know the implications of “being stable” by heart, but it’s not true that it will always return the same value. The reason for that is that, for most types, `null` is a valid value (although I imagine that the fact that exceptions can be thrown is also an issue).

For example, the following code is valid:

```scala
import scala.util.Random

val x: String = "foo"
def y: x.type = if (Random.nextBoolean()) x else null

println(y)
println(y)
println(y)
println(y)

```

> **[Scastie - An interactive playground for Scala.](https://scastie.scala-lang.org/iYUUFx0xSl6TAPIKOrsbaA)**
>
> import scala.util.Random
> 
> val x: String = "foo"
> def y: x.type = if (Random.nextBoolean()) x else null
> 
> println(y)
> println(y)
> println(y)
> println(y)

---

<div class="post-metadata">

**Author:** ![Sporarum](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sporarum/32/2545_2.png) [@Sporarum](https://contributors.scala-lang.org/u/Sporarum)\
**Post date:** [March 16, 2025, 3:16pm UTC](https://contributors.scala-lang.org/t/should-a-def-with-a-singleton-type-be-considered-stable/7075/3 "2025-03-16T15:16:23Z")

</div>

Oh wow, I would definitely not have expected that !

---

<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:** [March 17, 2025, 10:01am UTC](https://contributors.scala-lang.org/t/should-a-def-with-a-singleton-type-be-considered-stable/7075/4 "2025-03-17T10:01:02Z")

</div>

> [@JD557](#):
>
> for most types, `null` is a valid value

On the other hand, that same principle also applies to `val`s, and yet…

```scala
val x: "foo" = "foo"
def y: x.type = null
val z: x.type = null

println(y)
println(z)

```

> **[Scastie - An interactive playground for Scala.](https://scastie.scala-lang.org/BDcdhlW1RgCt8aBuaEHRTg)**
>
> val x: "foo" = "foo"
> def y: x.type = null
> val z: x.type = null
> 
> println(y)
> println(z)

---

<div class="post-metadata">

**Author:** ![Sporarum](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sporarum/32/2545_2.png) [@Sporarum](https://contributors.scala-lang.org/u/Sporarum)\
**Post date:** [March 17, 2025, 1:10pm UTC](https://contributors.scala-lang.org/t/should-a-def-with-a-singleton-type-be-considered-stable/7075/5 "2025-03-17T13:10:45Z")

</div>

Oh wow, I would definitely not … wait, I’m repeating myself

---

<div class="post-metadata">

**Author:** ![mberndt](https://contributors.scala-lang.org/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@mberndt](https://contributors.scala-lang.org/u/mberndt)\
**Post date:** [March 17, 2025, 3:30pm UTC](https://contributors.scala-lang.org/t/should-a-def-with-a-singleton-type-be-considered-stable/7075/6 "2025-03-17T15:30:00Z")

</div>

The obvious question here is: why do we need this? Why not just use a (lazy) val?

---

<div class="post-metadata">

**Author:** ![JD557](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/jd557/32/3004_2.png) [@JD557](https://contributors.scala-lang.org/u/JD557)\
**Post date:** [March 17, 2025, 3:59pm UTC](https://contributors.scala-lang.org/t/should-a-def-with-a-singleton-type-be-considered-stable/7075/7 "2025-03-17T15:59:32Z")

</div>

Not gonna lie, changing that example to have `val x: "foo" = null ` and having it work is pretty bonkers: [Scastie - An interactive playground for Scala.](https://scastie.scala-lang.org/5yqIHkffSx6oZEKJz0G1QA)

Although later versions of the compiler seem to trigger an error.

---

<div class="post-metadata">

**Author:** ![Sporarum](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sporarum/32/2545_2.png) [@Sporarum](https://contributors.scala-lang.org/u/Sporarum)\
**Post date:** [March 18, 2025, 9:11am UTC](https://contributors.scala-lang.org/t/should-a-def-with-a-singleton-type-be-considered-stable/7075/8 "2025-03-18T09:11:39Z")

</div>

> [@mberndt](#):
>
> The obvious question here is: why do we need this? Why not just use a (lazy) val?

A (lazy) val adds to the memory footprint of the class, whereas a def does not  
(An inline val may solve the issue sometimes, but not always)

It also feels like it should work, which is usually an indication it should

---

<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:** [March 18, 2025, 12:47pm UTC](https://contributors.scala-lang.org/t/should-a-def-with-a-singleton-type-be-considered-stable/7075/9 "2025-03-18T12:47:34Z")

</div>

if you use `transparent inline` then the compiler will do what you ask here
