You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When I try to create an extension for the class via implicit class and overload existing generic method, it fails with compilation error:
error: overloaded method value getAs with alternatives:
(fieldName: String)String <and>
(i: Int)String
cannot be applied to (FooField.type)
r.getAs[String](FooField)
While overloading normal (non-generic) method via implicit works fine. Tried on Scala 2.12.10. Link to scastie. What am I missing? Code:
traitRow {
// Two overloads for `getAs[T]`defgetAs[T](i: Int):TdefgetAs[T](fieldName: String):T// Two overloads for `get`defget(i: Int):Stringdefget(fieldName: String):String
}
traitField {
defcolumnName:StringdefcolumnDescription:String
}
caseobjectFooFieldextendsField {
defcolumnName:String="Foo"defcolumnDescription:String="Foo desc"
}
objectImplicits {
implicitclassRowEx(valr:Row) extendsAnyVal {
defgetAs[T](field: Field):T= r.getAs[T](field.columnName)
defget(field: Field):String= {
println(s"RowEx.get: field")
r.get(field.columnName)
}
}
}
objectMain {
importImplicits._// Create some instance of `Row`valr=newRow {
defgetAs[T](i: Int):T= i.toString.asInstanceOf[T]
defgetAs[T](fieldName: String):T= fieldName.toString.asInstanceOf[T]
defget(i: Int):String= i.toString
defget(fieldName: String):String= fieldName
}
defmain(args: Array[String]):Unit= {
// Call extension method => `RowEx.get`
println(r.get(FooField))
// Call extension method => `RowEx.get`// Won't compile with compilation error:/* overloaded method value getAs with alternatives: (fieldName: String)String (i: Int)String cannot be applied to (FooField.type)*/
println(r.getAs[String](FooField))
}
}
Uh oh!
There was an error while loading. Please reload this page.
When I try to create an extension for the class via implicit class and overload existing generic method, it fails with compilation error:
While overloading normal (non-generic) method via implicit works fine. Tried on
Scala 2.12.10
. Link to scastie. What am I missing? Code:Originally I created a question in StackOverflow
The text was updated successfully, but these errors were encountered: