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

SI-6276 Exclude var from trivial recursion warning #5391

Closed
wants to merge 1 commit into from

Conversation

som-snytt
Copy link
Contributor

var x: X = x is the new var x: X = _.

Unlike explicitly setting a default, it preserves any value
that was set earlier during initialization.

`var x: X = x` is the new `var x: X = _`.

Unlike explicitly setting a default, it preserves any value
that was set earlier during initialization.
@scala-jenkins scala-jenkins added this to the 2.12.1 milestone Sep 10, 2016
@SethTisue
Copy link
Member

I'm not sure how I feel about this...

@som-snytt
Copy link
Contributor Author

This must have come up on gitter or SO. In fact, I suddenly recall you wanted to eliminate underscore syntax? Maybe that was the context.

@SethTisue
Copy link
Member

SethTisue commented Oct 21, 2016

ah, the discussion about _ vs null.asInstanceOf[T]. I would like to reduce the number of cases where null can sneak into your code even though you didn't textually say null. This PR seems like a step backwards on that.

as for the "preserves any value that was set earlier during initialization" part, that gives my hives the heebie jeebies, or is that the other way around? indeed

scala> object O { x = 3; var x: Int = x; println(x) }
<console>:11: warning: variable x in object O does nothing other than call itself recursively
       object O { x = 3; var x: Int = x; println(x) }
                                      ^
<console>:11: warning: Reference to uninitialized variable x
       object O { x = 3; var x: Int = x; println(x) }
                  ^
defined object O

scala> O
3
res0: O.type = O$@3b90a30a

but is there a point to reducing this from 2 warnings to 1? I think it'd rather it printed 10 warnings.

maybe you've spent longer thinking about than I have and you actually have a case, but if so, I'm asking you to state that case. what's an example of code that it's actually good not to issue this warning for?

@SethTisue SethTisue modified the milestones: 2.12.2, 2.12.1 Nov 15, 2016
@som-snytt
Copy link
Contributor Author

som-snytt commented Dec 15, 2016

It can be useful to stash a value somewhere for debugging purposes, that is, to be read with a debugger.

With #5402

$ skala -Ywarn-unused
Welcome to Scala 2.12.2-20161212-232841-f048339 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_111).
Type in expressions for evaluation. Or try :help.

scala> object X { private var x: Int = _ }
<console>:11: warning: private var x in object X is never used
       object X { private var x: Int = _ }
                              ^
defined object X

scala> object X { private var x: Int = x }
<console>:11: warning: private setter of x in object X is never used
       object X { private var x: Int = x }
                              ^
<console>:11: warning: variable x in object X does nothing other than call itself recursively
       object X { private var x: Int = x }
                                       ^
defined object X

scala> object X { private var x: Int = x ; x = 42 }
<console>:11: warning: variable x in object X does nothing other than call itself recursively
       object X { private var x: Int = x ; x = 42 }
                                       ^
defined object X

The warning about private setter should suggest using a val, so I broke that message. (Edit: actually it always worked like that, only local vars get the val prompt.)

@adriaanm adriaanm self-assigned this Dec 19, 2016
@SethTisue
Copy link
Member

SethTisue commented Feb 10, 2017

further thoughts or opinions on this, anyone?

@som-snytt
Copy link
Contributor Author

I'm not anyone, but re-reading it again, it's pretty funny.

import language.scaryScala might take care of it.

@lrytz
Copy link
Member

lrytz commented Feb 13, 2017

I also don't feel like var x: X = x is something we should promote.

@adriaanm
Copy link
Contributor

I'm not sensing a lot of enthusiasm for this one. If the main use case is for debugging purposes, wouldn't it be desirable to have a warning remind you to remind that debug line before you submit your PR? Happy to reopen if someone feels strongly about getting this in.

@adriaanm adriaanm closed this Feb 16, 2017
@som-snytt
Copy link
Contributor Author

Thanks, I'll reopen when attempts to squash the underscore initializer break down in tears. The syntax from the dotty implicit args issue might work, namely, var x: X = implicit. Aha! Or implicitly. I'll have to consult @ merriam-webster.

@SethTisue SethTisue removed this from the 2.12.2 milestone Feb 22, 2017
@som-snytt som-snytt deleted the issue/nowarn-recursive-var branch December 19, 2020 21:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants