This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Type Inference Question

1 reply
Declan Conlon
Joined: 2009-08-12,
User offline. Last seen 42 years 45 weeks ago.

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

ewilligers
Joined: 2008-08-20,
User offline. Last seen 3 years 17 weeks ago.
Re: Type Inference Question

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"
}
}

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland