In the ValueSet, I also have apply(pairs : (String, ValueBase)*)implicit conversions from Int and String to ValueBase. If I applied ValueSet("a" -> 1, "b" -> 2), then the pairs are (String, Int)converted to (String, ValueBase), and it works fine. If I apply only to one pair, ValueSet("a" -> 1)then he says that there is no overload for (String,Int), i.e. It will not implicitly convert. I can hack this by adding apply[V <% ValueBase](p : (String, V))which works in a one-party case.
Why apply(pairs : (String, ValueBase)*)does it work with only one pair?
(Bonus question: adding extra apply () seems to solve the problem - is there a better solution? Is there something wrong with this solution?)
Here is a complete compiled example simplified from my actual code to try and show a minimal problem.
class ValueBase
case class ValueInt(val value : Int) extends ValueBase
case class ValueString(val value : String) extends ValueBase
case class ValuePair(val key : String, val value : ValueBase)
case class ValueSet(val value : List[ValuePair]) extends ValueBase
object ValueSet {
def apply(pairs : (String, ValueBase)*) : ValueSet = {
ValueSet(pairs.map(p => ValuePair(p._1, p._2)).toList)
}
def apply[V <% ValueBase](p : (String, V)) : ValueSet = {
ValueSet(List(ValuePair(p._1, p._2)))
}
}
object Sample {
implicit def int2value(i : Int) = ValueInt(i)
implicit def string2value(s : String) = ValueString(s)
val oneInt = ValueSet("a" -> 1)
val oneString = ValueSet("b" -> "c")
val twoInt = ValueSet("d" -> 2, "e" -> 3)
val twoTypes = ValueSet("f" -> 4, "g" -> "quick brown fox")
val oneInt2 = ValueSet(Pair("a", 1))
val twoTypes2 = ValueSet(Pair("f", 4), Pair("g", "quick brown fox"))
}
source
share