Scala type inference system has its limitations.
For example in the following classes. I'd like to have a factory FooFactory
that creates instances of Foo
and a FooChanger
that can modify instances created by the FooFactory
.
After I create my Foo
and my FooChanger
with the factory, I'd like to use the changer in objects created by the FooFactory
object, however, scala is not able to recognize that the type myOwnChanger.fooFactory.Foo
and factory.Foo
are the same.
A possible solution is to cast, or also, to use only factory.theFooChanger.fooFactory
in all client code. Both are ugly solutions and force the client to do something unnatural.
The question is: What can be done to avoid the problem for the client? Is there a way to tell the inferencer that for the rest of the code myOwnChanger.fooFactory.Foo
and factory.Foo
are the same?
class FooChangerFactory(val fooFactory: FooFactory) {
class FooChanger
def change(foo: fooFactory.Foo) = foo
}
class FooFactory(i: Int) {
class Foo private[FooFactory] ()
def create = new Foo
def theFooChanger = new FooChangerFactory(this)
}
val factory = new FooFactory(1)
val myFoo = factory.create
val myOwnChanger = factory.theFooChanger
myOwnChanger.change(myFoo) // error here! it expects myOwnChanger.fooFactory.Foo