Skip to content

Instantly share code, notes, and snippets.

@derekwyatt
Created January 11, 2014 20:56
Show Gist options
  • Save derekwyatt/8376659 to your computer and use it in GitHub Desktop.
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
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.
// 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