Skip to content

Instantly share code, notes, and snippets.

@tstone
Last active August 29, 2015 14:03
Show Gist options
  • Save tstone/cc229419898ddea0394d to your computer and use it in GitHub Desktop.
Save tstone/cc229419898ddea0394d to your computer and use it in GitHub Desktop.
class Foo(x: Int) {
def this(x: String) = this(x.toInt)
}
class Bar(x: Int)
object Bar {
def apply(x: String) = new Bar(x.toInt)
}
// in use...
val f = new Foo("15")
val b = Bar("15")
// ---------------------------------------------------------
// Approach #1
class RedisCache(client: RedisClient, keyPrefix: Option[String] = None) {
def this(host: String, port: Int, keyPrefix: Option[String] = None)(implicit system: ActorSystem) =
this(new RedisClient(host, port)(system), keyPrefix)
}
val r = new RedisCache("http://redis.example.com", 100) with AppLogger
// Approach #2
class RedisCache(client: RedisClient, keyPrefix: Option[String])
object RedisCache {
def apply(host: String, port: Int, log: LoggerLike, keyPrefix: Option[String] = None) =
new RedisCache(new RedisClient(host, port), keyPrefix) {
val logger = log
}
}
val r = RedisCache("http://redis.example.com", 100, AppLogger.logger)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment