question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`val`s in `final` classes don't get the same optimisation that `final val`s get

See original GitHub issue

Compiler version

3.0.0

Minimized code

class Creature {
  val range: Int = 10
  println(range)
}

class Ant1 extends Creature {
  override final val range: Int = 2
}

final class Ant2 extends Creature {
  override val range: Int = 2
}

object Test extends App {
  new Ant1
  new Ant2
}

Output

2
0

Expectation

2
2

(Mentioned on the side in #12570.)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
nicolasstuckicommented, Jul 26, 2021

Does the behavior of the uninitialized field access have strict semantics, or is it an undefined behavior?

Is the behavior of uninitialized fields sound? Consider val x: 2 = 2, if accessed before initialization it could yield 0 which is unsound because 0 <:< 2 does not hold. Otherwise, 0 would need to be a bottom type of all numerical constants which would break all type-level numeric computations.

1reaction
nicolasstuckicommented, Jul 27, 2021

Good. Then it is ok to optimize them and therefore have fewer cases with uninitialized accesses that return unexpected values

Read more comments on GitHub >

github_iconTop Results From Across the Web

Confusing semantics of uninitialized `final val` containing literal ...
Should vals in a final class not have the same optimisation/behaviour, though? They should. All reactions.
Read more >
scala - Why are `private val` and `private final val` different?
Yes, that's the reason for non-private vals, but private vals obviously can't be inlined in other classes and break compatibility in the same ......
Read more >
Are you sure your AnyVals don't instantiate?
So, it turns out that any kind of parametrized implicit resolution, even when it targets only the value class in question, causes instantiation....
Read more >
1) What is the output of the following Program
1) What is the output of the following Program? public class Q1 {. public static void main(String[] args) {. String a = "hello";....
Read more >
Lazy vals in immutable case classes - Question - Scala Users
I am using lazy vals for virtually all data fields in my immutable case classes. Is there ever a reason to use a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found