Created
November 23, 2015 09:11
-
-
Save stanislav-chetvertkov/99d649f5ce765859b898 to your computer and use it in GitHub Desktop.
Magnet pattern
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
// Problem: type erasure | |
def complete[T :Marshaller](status: StatusCode, obj: T): Unit | |
def complete(future: Future[HttpResponse]): Int | |
def complete(future: Future[StatusCode]): Int | |
// Usage of CompletionMagnet | |
def complete(magnet: CompletionMagnet): magnet.Result = magnet() | |
sealed trait CompletionMagnet { | |
type Result | |
def apply(): Result | |
} | |
object CompletionMagnet { | |
implicit def fromStatusObject[T :Marshaller](tuple: (StatusCode, T)) = | |
new CompletionMagnet { | |
type Result = Unit | |
def apply(): Result = ... // implementation using (StatusCode, T) tuple | |
} | |
implicit def fromHttpResponseFuture(future: Future[HttpResponse]) = | |
new CompletionMagnet { | |
type Result = Int | |
def apply(): Result = ... // implementation using future | |
} | |
implicit def fromStatusCodeFuture(future: Future[StatusCode]) = | |
new CompletionMagnet { | |
type Result = Int | |
def apply(): Result = ... // implementation using future | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment