Skip to content

Instantly share code, notes, and snippets.

@purijatin
Last active November 5, 2015 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save purijatin/d8134a7a26874eb5c168 to your computer and use it in GitHub Desktop.
Save purijatin/d8134a7a26874eb5c168 to your computer and use it in GitHub Desktop.
This is damn good
//source: http://www.cakesolutions.net/teamblogs/default-type-parameters-with-implicits-in-scala
object Prac {
trait DefaultsTo[Type, Default]
object DefaultsTo {
implicit def defaultDefaultsTo[T]: DefaultsTo[T, T] = null
implicit def fallback[T, D]: DefaultsTo[T, D] = null
}
type Def[Default] = {
type l[Type] = DefaultsTo[Type, Default]
}
type Document = String
class Storage[DocType: Def[Document]#l](name: String){
def t:DocType = ???
}
def main(args: Array[String]) {
val settings = new Storage("settings") // inferred as Table[Document]
val s:Document = settings.t
trait User
val users = new Storage[User]("users") // inferred as Table[User]
val ss:User = users.t
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment