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

Update FAQ on initialization order #3017

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

lrytz
Copy link
Member

@lrytz lrytz commented May 7, 2024

No description provided.

@lrytz lrytz requested a review from sjrd May 7, 2024 17:28
@SethTisue SethTisue self-requested a review May 20, 2024 20:48
Copy link
Member

@SethTisue SethTisue left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very nice improvements. I've picked a few nits


**-Xcheckinit**: Add runtime check to field accessors.
The [`-Ysafe-init` compiler flag](https://docs.scala-lang.org/scala3/reference/other-new-features/safe-initialization.html) in Scala 3 enables compiler warnings for accesses to uninitialized fields:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest mentioning here that the flag is experimental, like all -Y flags

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's like saying use it, but don't like use it. How about instead of experimental, we start saying "cutting-edge". Also, experimental has the narrow meaning for dotty that you need a flag and an experimental compiler to use the feature.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it will also be -Wsafe-init from 3.5

In Scala 2, the `-Xcheckinit` flag adds runtime checks in the generated bytecode to identify accesses of uninitialized fields.
The code then throws an exception rather than allowing a `null` (or `0` / `false` in the case of primitive types) to silently appear.
Note that these runtime checks only test code that is actually exectued at runtime.
The flag can be helpful to find accesses to uninitialized fields, but it should never be used in production due to its performance overhead.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The flag can be helpful to find accesses to uninitialized fields, but it should never be used in production due to its performance overhead.
The flag can be helpful to find accesses to uninitialized fields, but it should not be used in production due to its performance overhead.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"must never", and spoken with Germanic authority. People run debug in production all the time just in case there's a crash, which there always is.

Using it on the opening example:
In Scala 2, the `-Xcheckinit` flag adds runtime checks in the generated bytecode to identify accesses of uninitialized fields.
The code then throws an exception rather than allowing a `null` (or `0` / `false` in the case of primitive types) to silently appear.
Note that these runtime checks only test code that is actually exectued at runtime.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Note that these runtime checks only test code that is actually exectued at runtime.
Note that these runtime checks only test code that is actually executed at runtime.

@som-snytt
Copy link
Contributor

@SethTisue I suggest we don't wait for Lukas and definitely don't wait for Seb, who would incur more rounds of nits even pickier than previously surfaced.


Constant values are stricter than strict and earlier than early definitions and have even more limitations,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Omitting compile-time mechanisms to work around runtime issues seems important. Probably more is available in Scala 3 besides final vals.

Let's also respect that this was the original one-question FAQ. Maybe a minute of silence?

abstract class A {
val x1: String
val x2: String = "mom"
Note that using multiple lazy vals creates a new risk: cycles among lazy vals can result in a stack overflow on first access.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or dead lock in Scala 3 for thread-safe lazy vals.

class B extends A {
val x1: String = "hello"
final val x3 = "goodbye"
### Use a nested object
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't that basically the same solution as a lazy val?

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

Successfully merging this pull request may close these issues.

5 participants