Skip to content

Instantly share code, notes, and snippets.

@farmdawgnation
Created October 6, 2012 02:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save farmdawgnation/3843451 to your computer and use it in GitHub Desktop.
Save farmdawgnation/3843451 to your computer and use it in GitHub Desktop.
Some things scala can do
// This is going to be a really quick summary of things, but it should give you an idea.
// (At least of some of the unique aspects of scala.)
// EXAMPLE 1: Flow control via pattern matching
trait HasUserIdentifier {
// Anything mixing this in must have a userId method
// that returns a string
def userId:String
}
// special type of class
case class UserLoggedIn(userId:String, time:Date, ...) extends HasUserId
case class UserLoggedOut(userId:String, time:Date, ...) extends HasUserId
// Then we could write a method like so...
def handleUserEvent(event:HasUserId) = {
// Match statement
event match {
case UserLoggedIn(userId, time, ...) =>
doSomethingWithUserLoggedIn(userId)
......
case UserLoggedOut(userId, time, ...) =>
doSomethingWithUserLoggedOut(userId)
......
case _ => //default case that, in this situation, does nothing
}
}
// EXAMPLE 2: Option: No more nulls - Nulls are evil. They cause exceptions. We avoid them
// in Scala. Instead, we can use options for things that are... optional. Options are either
// Some or None.
val maybeAString = Some("string") // Some
// or
val maybeAString = None // None
val maybeAInt = Some(1)
// or
val maybeAInt = None
// You can also match on these
maybeAInt match {
case Some(number) =>
// do something with number
case _ =>
// do something else, maybe log an error
}
// EXAMPLE 2: Box: This isn't strictly Scala, but it is something implemented
// by Lift, the web framework we use at OpenStudy. Boxes have three basic possibilities:
// Full, Empty, or Failure. There's a fourth type called ParamFailure that has some extra
// bling, but we'll ignore that for now. Failures are typically used to indicate errors
maybeAString match {
case Full(text) =>
// Do something with text.
case Failure(message, _, _) =>
// If all we want to do is log the message, we can use underscores for the
// other two parameters here to indicate we just don't care. You'll see that
// _ is the swiss army knife of scala.
logger.error(message)
case _ =>
// Something else. Maybe an empty?
}
// EXAMPLE 3: For comprehensions. Possibly the best part, let's take this method.
def findUserForId(userId:Option[String]) = {
for {
userId <- userId
} yield {
// Find the user in the database
}
}
// This would return an Option[User], with a None if a None was passed in for the
// userId, and a Some[User] if a Some[String] was passed in. Make sense? Box
// also has some cool additions
def findUserForId(userId:Box[String]) = {
for {
userId <- userId ?~! "User ID is required."
} yield {
// Find the user in the database.
}
}
// This would return a Box[User]. If a Full[String] was passed in, it'll return
// a Full[User]. If an Empty was passed in, it'll return a Failure with the message
// "User ID is required." The calling code can then handle that Failure. Once again
// Box is a feature of Lift, not Scala - but good to see all the same. :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment