trait StringReader[T] {def fromString(input: String): T}object StringReader {def fromString[T: StringReader](input: String)(implicit reader: StringReader[T]): T =reader.fromString(input)implicit object IntStringReader extends StringReader[Int] {def fromString(input: String): Int = input.toInt}implicit object DoubleStringReader extends StringReader[Double] {def fromString(input: String): Double = input.toDouble}}import StringReader.fromStringcase class Token(intField: Int, doubleField: Double)val token = Token(intField = fromString("1"), doubleField = fromString("1.0"))//Error:(30, 46) ambiguous implicit values:// both object IntStringReader in object StringReader of type A$A42.this.StringReader.IntStringReader.type//and object DoubleStringReader in object StringReader of type A$A42.this.StringReader.DoubleStringReader.type//match expected type A$A42.this.StringReader[T]//lazy val token = Token(intField = fromString("1"), doubleField = fromString("1.0"))//^
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
object MappingRecord extends SfmRichSQLSyntaxSupport[MappingRecord] {override val tableName = "MAPPINGS"def apply(rn: ResultName[MappingRecord])(rs: WrappedResultSet): MappingRecord = {import TypeBinders._new MappingRecord(reference_id = rs.int(rn.reference_id),reference_type = rs.get(rn.reference_type),property_name = rs.string(rn.property_name))}}
def get[A: TypeBinder](columnLabel: String): A = {ensureCursor()wrapIfError(implicitly[TypeBinder[A]].apply(underlying, columnLabel))}
On Wed, May 13, 2015 at 7:18 AM, Volkan Yazıcı <volkan...@gmail.com> wrote:
Hello,Why Scala cannot infer the type of a parameter passed via a named argument. That is, in the following snippettrait StringReader[T] {def fromString(input: String): T}object StringReader {def fromString[T: StringReader](input: String)(implicit reader: StringReader[T]): T =
Here you are actually passing two implicit StringReaders. Either have an explicit (!) implicit, or use the [T: StringReader] syntax and access it via implicitly[StringReader[T]].
reader.fromString(input)implicit object IntStringReader extends StringReader[Int] {def fromString(input: String): Int = input.toInt}implicit object DoubleStringReader extends StringReader[Double] {def fromString(input: String): Double = input.toDouble}}import StringReader.fromStringcase class Token(intField: Int, doubleField: Double)val token = Token(intField = fromString("1"), doubleField = fromString("1.0"))//Error:(30, 46) ambiguous implicit values:// both object IntStringReader in object StringReader of type A$A42.this.StringReader.IntStringReader.type//and object DoubleStringReader in object StringReader of type A$A42.this.StringReader.DoubleStringReader.type//match expected type A$A42.this.StringReader[T]//lazy val token = Token(intField = fromString("1"), doubleField = fromString("1.0"))//^can't I avoid providing explicit type parameters to fromString calls? I mean, is there a way to make this work without using fromString[Int]("1")?Best.
--