scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
230 stars 21 forks source link

Type inference failure on constructor calls #336

Closed scabug closed 13 years ago

scabug commented 16 years ago

The following code fails to compile:

object Main{ implicit val bar = new Bar; implicit val foo = new Foo; }

class Bar { } class Foo (implicit val bar : Bar);

Giving the error "recursive value foo needs type".

However given that foo is the result of a constructor with all the type arguments fully specified (in this case there are none) there should never be any ambiguity in inferring the result type.

The reason it thinks this is recursive is presumably that val declarations are actually a letrec, and so implicit val declarations are in scope for their definition. For example, the following works:

class Bar(implicit val foo : Bar);

implicit val bar : Bar = new Bar;

(this results in bar.foo being null though. As a side note, I wish scalac could catch errors of this form at compile time, only allowing let recs inside a lazy expression).

However even in this case the type is totally unambiguous, and in the Foo case foo is not even recursive because it has the wrong type.

You can observe an effectively identical problem in the following case:

class Baz(baz : => Baz); val baz = new Baz(baz);

scabug commented 16 years ago

Imported From: https://issues.scala-lang.org/browse/SI-336?orig=1 Reporter: @DRMacIver

scabug commented 16 years ago

@DRMacIver said: Any news on this? It's really very annoying (particularly in SBinary where it happens in just about every method definition in the entire library).

scabug commented 16 years ago

@odersky said: Recent votes on the mailing list suggest that we should demand an explicit result type for all implicits. I believe this would also solve this problem.

scabug commented 16 years ago

@DRMacIver said: Well, kinda the opposite really. It would mandate the current behaviour of non-inference as required.

Would it be possible to consider statements of the form

def foo = new Bar;

as explicitly defining a result type without having to write it as

def foo : Bar = new Bar;
scabug commented 15 years ago

@odersky said: Milestone next_feature deleted

scabug commented 15 years ago

@paulp said: The supplied test case has worked since 2.7.2, so I think this should be closed. If the ancillary examples are of sufficient interest they should probably get their own tickets if they want much chance of attention.

scabug commented 15 years ago

@paulp said: I am closing this with the reporter's blessing. (He did not actually bless anything or anyone, but I am a liberal paraphraser.)

scabug commented 15 years ago

@DRMacIver said: I might have, but I'm explicitly forbidding from blessing things pending the results of my canonization.