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.

Compiler options -Ysafe-init and -Yexplicit-nulls do not report unsafe null inits in classes

See original GitHub issue

Compiler 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"
)

See also https://stackoverflow.com/questions/69612468/scala-3-ysafe-init-and-yexplicit-nulls-do-not-report-unsafe-null-inits-in-clas

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
olhotakcommented, Nov 11, 2021

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 of uninitialized would just be annotated with @unchecked, which is an unnecessary annoyance.

0reactions
ebruchezcommented, Feb 18, 2022

Today I set to demo the great Scala 3 null-safety abilities to someone, so I set:

"-Yexplicit-nulls",
"-Xfatal-warnings"

I wrote the following code in Scastie:

class D:
  var x: String = _

println(D().x)

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:

var x: String = _

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.

Read more comments on GitHub >

github_iconTop 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 >

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