Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type inference fails for singleton objects #1208

Closed
scabug opened this issue Aug 11, 2008 · 11 comments
Closed

Type inference fails for singleton objects #1208

scabug opened this issue Aug 11, 2008 · 11 comments

Comments

@scabug
Copy link

scabug commented Aug 11, 2008

The following fails with a type mismatch:

object Foo
val f: Option[Foo.type] = Some(Foo)

but succeeds with extra type annotation:

val f: Option[Foo.type] = Some[Foo.type](Foo)

This is annoying and I wish it were different :) Especially for the case with multiple type parameters, all of which can be inferred except for the singleton...

@scabug
Copy link
Author

scabug commented Aug 11, 2008

Imported From: https://issues.scala-lang.org/browse/SI-1208?orig=1
Reporter: Aaron Harnly (aaronharnly)

@scabug
Copy link
Author

scabug commented Aug 11, 2008

Aaron Harnly (aaronharnly) said:
Forgot to mention, this is in 2.7.1 and in trunk as of r15611.

@scabug
Copy link
Author

scabug commented Aug 11, 2008

@odersky said:
This has come up several times before. Changing this begavior would break lots of things, so I don't see it happening.

@scabug
Copy link
Author

scabug commented Aug 12, 2008

Geoffrey Alan Washburn (washburn) said:
I think this needs to be fixed eventually because it violates the principle that the inference algorithm should infer the most precise type possible.

@scabug
Copy link
Author

scabug commented Aug 12, 2008

@odersky said:
I refer to the spec:

The type of a designator is the type $$T$$ of the entity it refers to, with the
following exception: The type of a path (\sref{sec:paths}) $$p$$ which
occurs in a context where a stable type (\sref{sec:singleton-types}) is required is the
singleton type \lstinline@$$p$$.type@.

I invite everyone to change this rule, and observe what breaks!

@scabug
Copy link
Author

scabug commented Aug 28, 2008

Lauri Alanko (lealanko) said:
Inferring a more specific type than currently would break at least programs where that type is used in a non-positive position, in particular vars and type arguments to non-covariant type parameters.

Personally, I think it's a bad idea to infer a type into a non-positive position from a positive occurrence (i.e. the type of a value), and wouldn't mind requiring explicit type annotations/arguments for such situations, if that would make inference into positive positions work better. Granted, this would make local vars less practical, but personally I don't see much reason to make imperative programming too easy. :)

@scabug
Copy link
Author

scabug commented Aug 29, 2008

@odersky said:
I think discussions like this are fine, when done on the mailing list, but they should not be tickets. I am utterly overloaded already with fixing real bugs. Discussion about language design are better done using mailing lists and/or SIPS.

@scabug
Copy link
Author

scabug commented Jan 14, 2009

@odersky said:
Milestone next_feature deleted

@scabug
Copy link
Author

scabug commented Nov 29, 2010

@paulp said:
Reopening for lulz.

@scabug
Copy link
Author

scabug commented Nov 29, 2010

@paulp said:
(In r23622) "I invite everyone to change this rule, and observe what breaks!"

This much beloved comment from #1208 has been on my radar for two
years. The worm has turned. Closes #1208. The inferred type of an
object is now Foo.type instead of "object Foo". What once was this:

scala> val x: Map[Int, Singleton] = Map(1 -> None)
:5: error: type mismatch;
found : (Int, object None)
required: (Int, Singleton)

Now exudes a pleasing aura of workingness:

scala> val x: Map[Int, Singleton] = Map(1 -> None)
x: Map[Int,Singleton] = Map(1 -> None)

No review.

@scabug scabug closed this as completed May 18, 2011
@scabug
Copy link
Author

scabug commented Dec 3, 2014

@okomok said:
Does it mean "the types of modules are inferred"?
This behavior is not specified in the SLS?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant