Skip to content

Instantly share code, notes, and snippets.

@DeepSky8
Created April 8, 2014 16:38
Show Gist options
  • Save DeepSky8/10153250 to your computer and use it in GitHub Desktop.
Save DeepSky8/10153250 to your computer and use it in GitHub Desktop.
This should call a new instance of the class - but the first line isn't liked
val wager = "Great! How much do you want to wager?"
val leave = "Well, thanks for coming out today, partner."
val again = "I didn't quite catch that, sonny. You'll have to speak up."
val get = readLine()
class wagers
println(wager)
class leaves
println(leave)
class agains
println(again)
val gets = readLine()
continue(gets)
def continue(p: String): class = p match {
case y if (p == "Yes") => new wagers
case z if (p == "yes") => new wagers
case n if (p == "No") => new leaves
case m if (p == "no") => new leaves
case _ => new agains
}
@bkyrlach
Copy link

bkyrlach commented Apr 8, 2014

I like this code, and I think you're doing a good job. However, there still appears to be a little confusion here, so let's get at it.

  1. The word "class" is a keyword. It doesn't represent a type. This means that line 15 cannot compile, because you're trying to say that continue returns (gives back) a class, but instead you've given a nonsensical statement. In this case, it looks like what you really mean is String, however...

  2. You're using the new keyword, but what follows is not a class. When you say "new wagers" on line 16, this line doesn't make any sense to the Scala compiler. The new keyword must be followed by a class, but here, you've followed that keyword with a value (wagers is a value of type String). In this case, I think you meant to omit the keyword "new".

  3. Both you and Tom appear obsessed with if guards. :) I understand you're both still learning, I'm just trying to point out some ways to make the code better. In this case, the string literal "Yes" is a pattern. So instead of...

case y if (p == "Yes") => new wagers

I think you should just have...

case "Yes" => wagers

Another thing that might improve the code is I believe that String has a method toLowerCase that you can use to convert the string to lower case. So, if you said...

p.toLowerCase match { ... }

You could probably get rid of some duplicate cases.

Hope this helps. :)

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