Skip to content

Instantly share code, notes, and snippets.

@machisuji
Created December 12, 2011 21:45
Show Gist options
  • Select an option

  • Save machisuji/1469252 to your computer and use it in GitHub Desktop.

Select an option

Save machisuji/1469252 to your computer and use it in GitHub Desktop.
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
// ...
}
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