`val`s in `final` classes don't get the same optimisation that `final val`s get
See original GitHub issueCompiler 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:
- Created 2 years ago
- Reactions:1
- Comments:9 (9 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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 yield0
which is unsound because0 <:< 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.Good. Then it is ok to optimize them and therefore have fewer cases with uninitialized accesses that return unexpected values