| // Define the following traits and companion object | |
| // It's in Rapture Core (https://github.com/propensive/rapture-core) if you don't want to | |
| trait LowPriorityDefaultsTo { implicit def fallback[T, S]: DefaultsTo[T, S] = null } | |
| object DefaultsTo extends LowPriorityDefaultsTo { implicit def defaultDefaultsTo[T]: DefaultsTo[T, T] = null } | |
| trait DefaultsTo[T, S] | |
| // Then, assuming we want to specify a default for a type class like `Namer`, | |
| case class Namer[T](name: String) | |
| // where we have a couple of alternatives, | |
| implicit val stringNamer = Namer[String]("string") | |
| implicit val intNamer = Namer[Int]("int") | |
| // we define the default one for a particular method like this: | |
| def myMethod[T](implicit default: T DefaultsTo String, namer: Namer[T]) = namer.name | |
| // Let's try it out in the REPL: | |
| scala> myMethod | |
| res0: String = string | |
| scala> myMethod[Int] | |
| res1: String = int |
This comment has been minimized.
This comment has been minimized.
|
Cool! :) |
This comment has been minimized.
This comment has been minimized.
|
It's even more expectacular if you change it to |
This comment has been minimized.
This comment has been minimized.
|
Jon, i wonder why did you place To be sure this compiles on 2.11.7 trait DefaultsTo[Type, Default]
object DefaultsTo {
implicit def fallback[T, D]: DefaultsTo[T, D] = null
implicit def defaultDefaultsTo[T]: DefaultsTo[T, T] = null
}
trait Document
final class Table[DocType](val name: String)(implicit default: DocType DefaultsTo Document)
object Test {
val table = new Table("settings")
val table2 = new Table[String]("settings2")
} |
This comment has been minimized.
This comment has been minimized.
|
@4lex1v If that works, great! I tend to ignore the type-specificity rules and go straight with stacked traits, but that's a useful optimization! |
This comment has been minimized.
This comment has been minimized.
|
For documentation purpose, the link to a similar implementation in the mongo scala driver: https://github.com/mongodb/mongo-scala-driver/blob/76dad47c8dbfc98b59867355a966cf95ef72e556/bson/src/main/scala/org/mongodb/scala/bson/DefaultHelper.scala |
This comment has been minimized.
Very nice. I just added this to a much-used utility we have and it worked like a charm. Thanks!