Skip to content

Instantly share code, notes, and snippets.

@noahlz
Last active January 13, 2018 12:22
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noahlz/6048606 to your computer and use it in GitHub Desktop.
Save noahlz/6048606 to your computer and use it in GitHub Desktop.
Notes while working through "Scala for the Impatient"

Free excerpt from TypeSafe (pdf): Scala for the Impatient

Class Setters

I discovered the special syntax for a field setter:

class Person {
  private var privateAge = 0 // Make private and rename
  def age = privateAge
  def age_ = (newValue: Int) {
    if (newValue > privateAge) privateAge = newValue; // Can’t get younger
  }
}

Error:

<console>:10: error: not found: value newValue
         def age_ = (newValue: Int) {

It needed to be age_= (no space between the age_ and the =). I didn't realize that <identifier>_= is a special syntax. I'm used to = being a token for assignment.

Companion Objects in the Console

object Accounts {
  private var lastNumber = 0
  def newUniqueNumber = { lastNumber += 1; lastNumber}
}

// > defined module Accounts

class Accounts private(val id: Int, initialBalance: Double) {
  private var balance = initialBalance
}

// > defined class Accounts

But got this error:

warning: previously defined object Accounts is not a companion to class Accounts.
Companions must be defined together; you may wish to use :paste mode for this.

Don't bother using :paste if you are in the IntelliJ Scala console, as I was. You exit paste-mode with Ctrl-d, but IntelliJ captures that keystroke, so your only option is to quit the console process.

def needs =

def doWhat(color: TrafficLightColor.Value)  {
  if (color == TrafficLightColor.Red) "Stop"
  else if (color == TrafficLightColor.Green)  "Go"
  else if (color == TrafficLightColor.Yellow)  "Slow Down!" 
  else throw new IllegalArgumentException("error")
}

Error:

<console>:7: error: not found: value TrafficLightColor
       def doWhat(color: TrafficLightColor.Value)  {
                         ^
<console>:8: warning: a pure expression does nothing in statement position; you may be omitting necessary parentheses
         if (color == TrafficLightColor.Red) "Stop"
                                             ^
<console>:9: warning: a pure expression does nothing in statement position; you may be omitting necessary parentheses
         else if (color == TrafficLightColor.Green)  "Go"
                                                     ^
<console>:10: warning: a pure expression does nothing in statement position; you may be omitting necessary parentheses
         else if (color == TrafficLightColor.Yellow)  "Slow Down!" 

The error misled me into thinking I was doing the if/else block wrong (btw, I am aware that pattern matching is more idiomatic). In fact, the problem was that def requires an =, i.e. def doWhat(color: TrafficLightColor.Value) = { ...

Googling for warning: a pure expression does nothing in statement position; you may be omitting necessary parentheses did not yield any useful results, so if you found this page searching for the meaning of that error, I hope this helped you.

@jordanrobinson
Copy link

so if you found this page searching for the meaning of that error, I hope this helped you.

It did, thanks!

@scottashipp
Copy link

"..so if you found this page searching for the meaning of that error, I hope this helped you."

Exactly right on - this was my problem.

@som-snytt
Copy link

som-snytt commented Jul 6, 2016

For pasting in REPL, you can more recently:

scala> :pa < EOF
// Entering paste mode (EOF to finish)

class X
object X
EOF

// Exiting paste mode, now interpreting.

defined class X
defined object X

Also -Ywarn-value-discard helps for cases like unintended missing = ("procedure syntax") which is deprecated now under -Xfuture.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment