Created
December 12, 2011 21:45
-
-
Save machisuji/1469252 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| trait MessageCompanion[S <: Message, T] /* ... */ { | |
| def unapply(in: Input): Option[T] = { | |
| if (isDefinedAt(in)) { | |
| val result = Some(read(in)) | |
| checkComplete(in) | |
| result | |
| } else None | |
| } | |
| def read(in: Input): T | |
| // ... | |
| } |
This file contains hidden or 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
| class Request(val text: String, num: Int) extends Message { /* ... */ } | |
| class Ping extends Message { /* ... */ } | |
| object RequestCompanion extends MessageCompanion[Request, (String, int)] { /* ... */ } | |
| object PingCompanion extends MessageCompanion[Ping, ???] { /* ... */ } | |
| val in: Input = // ... | |
| in match { | |
| case RequestMessage(text, num) => println(num + " times " + text + "!") | |
| case PingMessage() => println("ping") | |
| // .. | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment