In the following session with scala 2.10.0-M7:
scala> trait A
defined trait A
scala> class B extends A
defined class B
scala> class C extends A
defi
I feel like sschaef version isn't precise enough. I don't know scala much (in fact, I never used it) but I don't think it depends on the way the function is implemented. I also didn't understood the "the typer goes from left to right" thing either.
I don't have a recent version of Scala, so I can't test on your example venechka, but I think the typing error/limitation can be circumvented by adding some type annotations. For exemple here is sschaef's example :
scala> List(1).foldLeft(Nil)((xs,x) => x::xs)
:8: error: type mismatch;
found : List[Int]
required: scala.collection.immutable.Nil.type
List(1).foldLeft(Nil)((xs,x) => x::xs)
^
scala> List(1).foldLeft(Nil : List[Int])((xs,x) => x::xs)
res1: List[Int] = List(1)
And I believe you could do the same on your example by doing something like :
Some(0).fold(new B : A){_=>new C}
Again, I think it's a limitation of Scala typer (probably due to the presence of subtyping), but I would have to look around before affirming that strongly.
Anyway, adding type annotations here and there should do the trick for you, so enjoy!
EDIT: Oh, sschaef edited his answer to have some explanations which probably will invalidate what I'm saying about the reason of this behavior. But it doesn't change the fact that type annotations will solve your problem. So I will just let this message in place.