Skip to content

Instantly share code, notes, and snippets.

@jessitron
Created June 15, 2013 17:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jessitron/5788968 to your computer and use it in GitHub Desktop.
Save jessitron/5788968 to your computer and use it in GitHub Desktop.
This is in response to a giant twitter thread. Thanks to @CraigBuchek, @brianbuttonxp, @adkron, @heathborders, and @marioaquino for the discussion. Post is at blog.jessitron.com, titled "What's dirtier than comments? Exceptions!"
case class Error(message:String)
case class AuthLevel(value: Int)
object AuthLevel {
val GUEST = AuthLevel(0)
}
class SettingGetter {
def get_setting(settingName: String): Either[Error, String] = Right("Fred")
}
class Authorizer {
def getAuthorizationLevel(user: String): AuthLevel = AuthLevel(4)
}
class ForBlogPost {
def getAuthLevel(c : SettingGetter,
authorizer: Authorizer): Either[Error, AuthLevel] = {
val possibleUser = c.get_setting("username")
val possibleAuth = possibleUser.right.map{ user =>
authorizer.getAuthorizationLevel(user) }
possibleAuth.joinRight
}
def doSomethingWithAuthLevel() {
val possibleAuth = getAuthLevel(new SettingGetter, new Authorizer)
possibleAuth.left.foreach(error => log(error))
val authLevel: AuthLevel = possibleAuth.right.getOrElse(AuthLevel.GUEST)
}
def log(a: Any) { println(a) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment