I want to use a generic function that returns the "default value" for a given type. I have implemented this with the help of the following case class construct:
case class DefaultOp[T](op: () => T)
implicit val defaultString = DefaultOp[String](() => "")
implicit val defaultInt = DefaultOp[Int](() => 0)
implicit val defaultFloat = DefaultOp[Float](() => 0.0f)
implicit val defaultDouble = DefaultOp[Double](() => 0.0d)
// ...
def default[T]()(implicit ev: DefaultOp[T]): T = ev.op()
That works fine, but I would like to extend it to also work on Types constructed from a type constructor. For instance, I would like to be the default value for any option type to be "None" and for any list type to be Nil. How can I implementd this? My first attemt:
implicit val defaultOption = DefaultOp[Option[_]](() => None)
compiles, but it does not compile when I use it in an application:
error: could not find implicit value for parameter ev: DefaultOp[Option[Float]]
var test: Option[Float] = default[Option[Float]]
^
one error found
Any suggestion how this can be made working?
Thanks!