8000 Save FAQ entry from paulp/scala-faq on abstract value troubles by Blaisorblade · Pull Request #270 · scala/docs.scala-lang · GitHub
[go: up one dir, main page]

Skip to content

Save FAQ entry from paulp/scala-faq on abstract value troubles #270

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

Merged
merged 3 commits into from
Nov 23, 2013
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix sectioning
  • Loading branch information
Blaisorblade committed Nov 23, 2013
commit 9f9804e8e7d7f12a0207c7b3743eb844dfe4ba15
8 changes: 5 additions & 3 deletions tutorials/FAQ/initialization-order.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ scala.UninitializedFieldError: Uninitialized field: a.scala: 13
at C.<init>(a.scala:12)
```

### Solutions ###

Approaches for avoiding null values include:

#### Use lazy vals. ####
#### Use lazy vals ####

```scala
abstract class A {
Expand Down Expand Up @@ -94,7 +96,7 @@ An exception during initialization of a lazy val will cause the right hand side

Note that using multiple lazy vals creates a new risk: cycles among lazy vals can result in a stack overflow on first access.

#### Use early definitions. ####
#### Use early definitions ####
```scala
abstract class A {
val x1: String
Expand All @@ -120,7 +122,7 @@ class C extends {

Early definitions are a bit unwieldy, there are limitations as to what can appear and what can be referenced in an early definitions block, and they don't compose as well as lazy vals: but if a lazy val is undesirable, they present another option. They are specified in SLS 5.1.6.

#### Use constant value definitions. ####
#### Use constant value definitions ####
```scala
abstract class A {
val x1: String
Expand Down
0