- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Confusing type mismatch error
Mon, 2011-05-09, 04:55
Hi all,
I have just started to learn scala, so please forgive my ignorance if this looks naive. Here is the code:
trait tr { def foo(t: String) = println(t) }
class AbstractClass { def bar[U](a: U)}
class ConcreteClass extends AbstractClass with tr { override def bar[String](a: String) = foo(a) // type mismatch; found : String(in method bar) required: java.lang.String}
I don't understand why there is a type mismatch here. I'm using the scala plugin for eclipse, and can someone explain what does the error message mean? what is String(in method bar)?
I have just started to learn scala, so please forgive my ignorance if this looks naive. Here is the code:
trait tr { def foo(t: String) = println(t) }
class AbstractClass { def bar[U](a: U)}
class ConcreteClass extends AbstractClass with tr { override def bar[String](a: String) = foo(a) // type mismatch; found : String(in method bar) required: java.lang.String}
I don't understand why there is a type mismatch here. I'm using the scala plugin for eclipse, and can someone explain what does the error message mean? what is String(in method bar)?
Mon, 2011-05-09, 06:07
#2
Re: Confusing type mismatch error
On Monday, May 9, 2011 12:31:33 AM UTC-4, samreid wrote:
Similar code follows, which (I have a feeling) may be more appropriate depending on the context. It's probably more likely that the desire is to have the type declaration apply to all methods using that type:
abstract class AbstractClass[U] { def bar(a: U)}
class ConcreteClass extends AbstractClass[String] with tr { override def bar(a: String) = foo(a)}
trait tr {def foo(t: String) = println(t)}
abstract class AbstractClass {
def bar[U <: String](a: U)
}
class ConcreteClass extends AbstractClass with tr {
override def bar[X <: String](a: X) = foo(a)
}
Similar code follows, which (I have a feeling) may be more appropriate depending on the context. It's probably more likely that the desire is to have the type declaration apply to all methods using that type:
abstract class AbstractClass[U] { def bar(a: U)}
class ConcreteClass extends AbstractClass[String] with tr { override def bar(a: String) = foo(a)}
trait tr {def foo(t: String) = println(t)}
abstract class AbstractClass {
def bar[U <: String](a: U)
}
class ConcreteClass extends AbstractClass with tr {
override def bar[X <: String](a: X) = foo(a)
}
object Tester {
def main(args: Array[String]) {
new ConcreteClass().bar("hello")
}
}
//Prints "hello"