Created
January 11, 2014 20:56
-
-
Save derekwyatt/8376659 to your computer and use it in GitHub Desktop.
Is the magnet pattern the only decent way to get around this limitation in the Scala compiler? `... multiple overloaded alternatives of method set define default arguments.` http://spray.io/blog/2012-12-13-the-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
def set(key: String, value: String)(implicit ec: ExecutionContext, jitter: TTLJitter = NoJitter): Future[CAS] | |
def set(key: String, value: ByteString)(implicit ec: ExecutionContext, jitter: TTLJitter = NoJitter): Future[CAS] | |
// Compiler error: ... multiple overloaded alternatives of method set define default arguments. |
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
// Simplifiied magnet that doesn't have a variant return type | |
sealed trait ByteStringMagnet { | |
val bs: ByteString | |
def apply(f: ByteString => Future[CAS]): Future[CAS] = f(bs) | |
} | |
object ByteStringMagnet { | |
implicit def fromString(s: String): ByteStringMagnet = new ByteStringMagnet { | |
val bs = ByteString(s) | |
} | |
implicit def fromByteString(bytes: ByteString): ByteStringMagnet = new ByteStringMagnet { | |
val bs = bytes | |
} | |
} | |
def set(key: String, magnet: ByteStringMagnet)(implicit ec: ExecutionContext, jitter: TTLJitter: NoJitter): Future[CAS] = | |
magnet { value => | |
doSomethingWithValueThatReturnsFutureCAS(value) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment