Compiler options -Ysafe-init and -Yexplicit-nulls do not report unsafe null inits in classes
See original GitHub issueCompiler version
3.0.2
Minimized code
class NullFoo:
var s: String = _
override def toString: String = s"Foo:s=$s"
object NullFoo:
def main(args: Array[String]): Unit =
println(new NullFoo)
Output
Foo:s=null
Expectation
With these compiler options, I would expect a compile error:
scalacOptions ++= Seq(
"-Ysafe-init", // renamed from -Ycheck-init, still present in official docs!
"-Yexplicit-nulls",
"-Xfatal-warnings"
)
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
python - Setting default/empty attributes for user classes in __init
Question: When I am creating a new class, should I set all instance attributes in __init__ , even if they are None and...
Read more >dataclasses — Data Classes — Python 3.11.1 documentation
Although not recommended, you can force dataclass() to create a __hash__() method with unsafe_hash=True . This might be the case if your class...
Read more >__init__ in Python - GeeksforGeeks
The task of constructors is to initialize(assign values) to the data members of the class when an object of the class is created....
Read more >Classes - Object-Oriented Programming in Python
A function performs an action using some set of input parameters. Not all functions are applicable to all kinds of data. Classes are...
Read more >Python Class Constructors: Control Your Object Instantiation
Class constructors are a fundamental part of object-oriented programming in Python. They allow you to create and properly initialize objects ...
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 FreeTop 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
Top GitHub Comments
This was discussed in the LAMP meeting today. The consensus there was that:
var x: String = _
should trigger a deprecation warning.var x: String = uninitialized
should not trigger a warning because otherwise all real-world uses ofuninitialized
would just be annotated with@unchecked
, which is an unnecessary annoyance.Today I set to demo the great Scala 3 null-safety abilities to someone, so I set:
I wrote the following code in Scastie:
And of course I got a
null
printed out, which is what this issue is about. It’s a disappointing first encounter with null-safety, I have to say! So +1 to do something about it really soon.This said, why would:
only trigger a deprecation warning? After all, I enabled explicit nulls AND safe initialization. I’d prefer it if this was an error fair and square when one or both of these option are enabled.