# Emulating a \`super\` of values

**URL:** <https://contributors.scala-lang.org/t/emulating-a-super-of-values/3681>\
**Category:** Language Design\
**Created:** [September 10, 2019, 3:46pm UTC](https://contributors.scala-lang.org/t/emulating-a-super-of-values/3681 "2019-09-10T15:46:47Z")\
**Posts on this page:** 12\
**Page:** 1

<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:** [September 10, 2019, 3:46pm UTC](https://contributors.scala-lang.org/t/emulating-a-super-of-values/3681/1 "2019-09-10T15:46:48Z")

</div>

Currently we cannot refer to a `super` of values. I propose that the compiler will emulate this by replacing a value with an inlined definition and a private value.  
For example, we can replace:

```auto
class Foo {
  val f = 0
}

class Bar extends Foo {
  override val f = super.f + 1 //error
}

```

with this:

```auto
class Foo {
  private val _f = 0
  @inline def f = _f
}

class Bar extends Foo {
  private val _f = super.f + 1
  @inline override def f = _f
}

```

---

<div class="post-metadata">

**Author:** ![scottcarey](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/scottcarey/32/843_2.png) [@scottcarey](https://contributors.scala-lang.org/u/scottcarey)\
**Post date:** [September 10, 2019, 4:01pm UTC](https://contributors.scala-lang.org/t/emulating-a-super-of-values/3681/2 "2019-09-10T16:01:11Z")

</div>

That doesnt appear to work with separate compilation. It may only be workable if the parent is sealed and yhe child in the same file.

---

<div class="post-metadata">

**Author:** ![scottcarey](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/scottcarey/32/843_2.png) [@scottcarey](https://contributors.scala-lang.org/u/scottcarey)\
**Post date:** [September 10, 2019, 4:06pm UTC](https://contributors.scala-lang.org/t/emulating-a-super-of-values/3681/3 "2019-09-10T16:06:39Z")

</div>

To be more specific, I am assuming the inner val is private and thus doesnt have an accessor by default and is hidden from children, and the proposed one is added for children after it is needed.

Vals are private for good reason, exposing them to child classes should be explict.

---

<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:** [September 10, 2019, 4:08pm UTC](https://contributors.scala-lang.org/t/emulating-a-super-of-values/3681/4 "2019-09-10T16:08:40Z")

</div>

Is it also true for lazy vals?

---

<div class="post-metadata">

**Author:** ![curoli](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/curoli/32/1985_2.png) [@curoli](https://contributors.scala-lang.org/u/curoli)\
**Post date:** [September 10, 2019, 4:28pm UTC](https://contributors.scala-lang.org/t/emulating-a-super-of-values/3681/5 "2019-09-10T16:28:30Z")

</div>

Why do you want to override a val?

I’ve been following the rule that if it may be overridden, it should be a def.

---

<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:** [September 10, 2019, 4:43pm UTC](https://contributors.scala-lang.org/t/emulating-a-super-of-values/3681/6 "2019-09-10T16:43:53Z")

</div>

> [@curoli](#):
>
> Why do you want to override a val?

I want it evaluated once

---

<div class="post-metadata">

**Author:** ![scottcarey](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/scottcarey/32/843_2.png) [@scottcarey](https://contributors.scala-lang.org/u/scottcarey)\
**Post date:** [September 10, 2019, 4:56pm UTC](https://contributors.scala-lang.org/t/emulating-a-super-of-values/3681/7 "2019-09-10T16:56:33Z")

</div>

Lazy val always has an accessor.

I may be mixing up private and private[this] a bit in my previous reply.  
For non-lazy val, I have seen some discussion about the presence or absence of accessors when private of late: [Change `private` to mean `private[this]`?](https://contributors.scala-lang.org/t/change-private-to-mean-private-this/3493)

A truly private val doesn’t need a byte code level accessor in most cases (more complex cases, like nested classes may need accessors or bridges).

At the bytecode level, accessors can have the same name as the class field – the uniform access principle is at the Scala language level, not the JVM level, where fields and methods have distinct namespaces. So in some sense, your proposal already exists when there are accessors (e.g. public val) but super is still not allowed.

Accessor or not, I think accessing a super val when defining a child can be made to work if its protected or public or (maybe) in a sealed hierarchy.  
I suspect this would make initialization issues more common if used heavily though – even though subclasses are initialized after their parents, there are many ways to get in trouble, and vals on traits would be problematic.

For your exmple specifically, if you “override” a val, you are really creating a new val and overriding the def accessor to redirect from the parents val to the childs while simultaneously providing the initial value. The actual val from the parent still exists in the class – at the bytecode level children can only append fields to a class, they can’t override them.

---

<div class="post-metadata">

**Author:** ![scottcarey](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/scottcarey/32/843_2.png) [@scottcarey](https://contributors.scala-lang.org/u/scottcarey)\
**Post date:** [September 10, 2019, 5:04pm UTC](https://contributors.scala-lang.org/t/emulating-a-super-of-values/3681/8 "2019-09-10T17:04:54Z")

</div>

> [@soronpo](#):
>
> I want it evaluated once

override a def with a lazy val.

If the parent needs to be evaluated once too, you may need a trait with the def on it that is overriden.

---

<div class="post-metadata">

**Author:** ![curoli](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/curoli/32/1985_2.png) [@curoli](https://contributors.scala-lang.org/u/curoli)\
**Post date:** [September 10, 2019, 7:40pm UTC](https://contributors.scala-lang.org/t/emulating-a-super-of-values/3681/9 "2019-09-10T19:40:44Z")

</div>

How about, instead of overriding the val, override a def that initializes it?

**class Foo {**

\*\* lazy val f: Int = initF\*\*

\*\* protected def initF: Int = 0\*\*

**}**

**class Bar extends Foo {**

\*\* override protected def initF: Int = super.initF + 1\*\*

**}**

---

<div class="post-metadata">

**Author:** ![Ichoran](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/ichoran/32/40_2.png) [@Ichoran](https://contributors.scala-lang.org/u/Ichoran)\
**Post date:** [September 10, 2019, 8:01pm UTC](https://contributors.scala-lang.org/t/emulating-a-super-of-values/3681/10 "2019-09-10T20:01:51Z")

</div>

Overriding a protected def is what I do when I need this pattern.

---

<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:** [September 11, 2019, 12:30am UTC](https://contributors.scala-lang.org/t/emulating-a-super-of-values/3681/11 "2019-09-11T00:30:16Z")

</div>

One limitation currently is you cannot override a `lazy val` with another `lazy val`.

I can’t see any reason why this limitation has to be in place. In fact, Mill explicitly does some macro magic to turn its `def foo = T{ ... }` tasks into `lazy val` semantics, and it works great!

---

<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:** [September 11, 2019, 4:57am UTC](https://contributors.scala-lang.org/t/emulating-a-super-of-values/3681/12 "2019-09-11T04:57:32Z")

</div>

That’s fine, if you assume there is only a single access to `super.initF`. I would rather this limitation didn’t exist in the compiler if it’s possible to do so.
