-
-
Save lazyval/5313079 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// it's good style to mark such objects/classes with sealed to guide complier | |
// so it will check if match is exhaustive (all possible cases covered) | |
sealed abstract class Response | |
sealed abstract class Reason | |
abstract class TokenType | |
case class Token(t: TokenType) extends Response | |
case class Problem(reason: Reason) extends Response | |
case object InvalidResponse extends Reason | |
case object Connection extends Reason | |
// note that order of this classes in mattern matching matters | |
class OneTime extends TokenType | |
class Refreshable extends OneTime { | |
def refresh() = ??? | |
} | |
/* | |
foo match { | |
case Token(t: Refreshable) => do something with t | |
case Problem(reason) => reason match { | |
case InvalidResponse => ... | |
case ConnectionFailure => ... | |
} | |
_ => // just an example: Token(OneTime) will go there | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment