Created
November 10, 2015 11:25
-
-
Save 4lex1v/e559da3b1806dd4696cd to your computer and use it in GitHub Desktop.
Using default type parameter to drive type inference and a type class instance resultion
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
trait DefaultsTo[Type, Default] | |
object DefaultsTo { | |
implicit def defaultDefaultsTo[T]: DefaultsTo[T, T] = null | |
implicit def fallback[T, D]: DefaultsTo[T, D] = null | |
} | |
trait Document | |
trait Reader[A] | |
object Reader { | |
implicit def docReader: Reader[Document] = | |
new Reader[Document] {} | |
} | |
class Storage[DocType](name: String)(implicit val r: Reader[DocType], default: DefaultsTo[DocType, Document]) | |
/** | |
* Inserting the above into the Scala repl, the result should be the following (Scala 2.11.7) | |
*/ | |
scala> val mongo = new Storage("mongo") | |
mongo: Storage[Document] = Storage@221af3c0 | |
// Resolved implicit value | |
scala> mongo.r | |
res0: Reader[Document] = Reader$$anon$1@57d5872c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment