Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Reference to uninitialized value" warning not produced for more complicated reference patterns #367

Closed
cnestor opened this issue Apr 7, 2017 · 3 comments

Comments

@cnestor
Copy link

cnestor commented Apr 7, 2017

Hey guys! Not sure how hard this would be to fix, but I thought I would bring it up.

//produces compiler warning
class C1 {
  val x = y
  val y = 1
}

//no warning :(
class C2 {
  val x = getY
  val y = 1
  def getY = y
}

//no warning either :(
trait T {
  val y: Int
  val x: Int = y
}

class C3 extends T {
  val y = 1
}

This is a really easy thing to do accidentally if you're moving things around and it can lead to a class that throws a NullPointerException whenever it is instantiated.

Related - #221
IMO it would be better to produce an error than a warning in general, especially since classes/strings/collections/misc non-Int values default to null, which seems to run very counter to the spirit of Scala. ¯\_ (ツ) _/¯

@SethTisue
Copy link
Member

this has been an area of active discussion over the years — you might look for related tickets in scala/bug. it is a difficult design and implementation problem. I doubt anything further will change in this area in Scala 2. not sure what the current status over in lampepfl/dotty is

@arpit1543
Copy link

I have experienced the same Error while compiling the code.
All i did was just moved around the code and error was gone.

@dwijnand
Copy link
Member

If you want, this PR in Dotty: scala/scala3#7789 is a proposal for an experimental -Ycheck-init to check that initialisation is safe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants