Scala code:
trait Converter[S, T] {
def convert(source: S): T
}
class StringDuplicatorConverter extends Converter[Integer, String] {
override def convert(source: Integer): String = {
source.toString + source.toString
}
}
// whatever, the point is to show potentially many 'converters'
// from Integer (for example) to something
class SomeOtherConverter extends Converter[Integer, User] {
override def convert(source: Integer): User = {
User(source)
}
}
trait ConverterProvider {
def getConverter[N]: Converter[Integer, N]
}
class MyClass extends ConverterProvider {
override def getConverter[N]: Converter[Integer, N] = {
new StringDuplicatorConverter()
}
}
gives
Error:(17, 5) type mismatch;
found : StringDuplicatorConverter
required: Converter[Integer,N]
new StringDuplicatorConverter()
It may be that what you really want is for each
ConverterProvider
to provide a converter to a specific type (otherwise the definition ofMyClass
doesn't make much sense: it should return different converters for differentN
, not alwaysStringDuplicatorConverter
). If so, the correct definition is