I've the following class-object
class DefaultValue[+A](val default: A)
object DefaultValue {
implicit object DefaultDouble extends DefaultValue[Double](-1.0)
implicit object DefaultFloat extends DefaultValue[Float](-1f)
implicit object DefaultInt extends DefaultValue[Int](-1)
implicit object DefaultBoolean extends DefaultValue[Boolean](false)
def value[A](implicit value: DefaultValue[A]): A = value.default
}
To use this I call DefaultValue.value[Int]
or DefaultValue.value[Double]
etc
However, I need the ability to call DefaultValue.value[Int]
when I have user input as "Int"
: the typename written in String, and similarly for others.
I wish to do this without pattern-matching, as it would essentially beat the purpose of inserting TypeTag in the function. Is there a better way to do this, perhaps using Scala Reflection?
One problem with that is that you won't be able to get a good return type.
Any
orDefaultValue[_]
is the best you can get.You can use reflection to write something like this:
This will work for
implicit object
s. If you want it to work withimplicit val
s (e.g.implicit val DefaultString = new DefaultValue[String]("~")
), you'd have to add code for this case:But actually, I think, it's not a bad idea to go for a
Map
with the correspondence:This approach is probably faster and really not that hard to maintain. Also, it would be possible to not require direct correspondence between object name and user input, or to have several possible strings for a single
DefaultValue
, etc.Also, if you declare the base class as
sealed
, and extend it only withobject
s, you could streamline the creation of thisMap
: