- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Type Inference Question
Wed, 2009-08-12, 19:40
I haven't worked through the full semantics of the type inference in
my following scenario, but can someone let me know why the type
variable T in class B is not bound to String and requires the type
definition in class B? Can't this be inferred from the return type of
doDerivedInit in class B?
abstract class A {
type T
val generigvar : T = init
def init = {
doDerivedInit
}
def doDerivedInit : T
}
class B extends A {
type T = String
override def doDerivedInit = {
"Some String"
}
}
Declan Conlon
Declan Conlon wrote:
> but can someone let me know why the type
> variable T in class B is not bound to String and requires the type
> definition in class B? Can't this be inferred from the return type of
> doDerivedInit in class B?
Type T can be anything that String can be converted to:-
class BA extends A {
type T = AnyRef
def doDerivedInit = {
"Some String"
}
}
class BU extends A {
type T = Unit
def doDerivedInit = {
"Some String"
}
}