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

List("ab").toSet.head.head won't compile #7743

Closed
scabug opened this issue Aug 12, 2013 · 15 comments
Closed

List("ab").toSet.head.head won't compile #7743

scabug opened this issue Aug 12, 2013 · 15 comments
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) infer typer

Comments

@scabug
Copy link

scabug commented Aug 12, 2013

scala> List("ab").toSet.head.head
<console>:8: error: value head is not a member of type parameter B
              List("ab").toSet.head.head
@scabug
Copy link
Author

scabug commented Aug 12, 2013

Imported From: https://issues.scala-lang.org/browse/SI-7743?orig=1
Reporter: Sagie Davidovich (sagie.d)
Affected Versions: 2.10.2

@scabug
Copy link
Author

scabug commented Aug 12, 2013

Tomer Gabel (holograph) said:
Can be worked around with:

scala> val a=List("ab").toSet.head
a: String = ab

scala> a.head
res2: Char = a

@scabug
Copy link
Author

scabug commented Aug 12, 2013

@som-snytt said:
scala> List("ab").to[Set].head.head
res1: Char = a

@scabug
Copy link
Author

scabug commented Aug 12, 2013

Tomer Gabel (holograph) said:
A.P: Which Scala version do you use? In my case:

arilou:incubator-curator tomer$ scala -version
Scala code runner version 2.10.2 -- Copyright 2002-2013, LAMP/EPFL

@scabug
Copy link
Author

scabug commented Aug 12, 2013

Tomer Gabel (holograph) said:
Also, your code sample is different :-) (toSet -> to[Set])

@scabug
Copy link
Author

scabug commented Dec 5, 2013

@SethTisue said:
I think this is just another version of this old chestnut: http://stackoverflow.com/questions/5544536/type-inference-on-set-failing

the least intrusive workaround IMO is List("ab").toSet[String].head.head

@scabug
Copy link
Author

scabug commented Aug 9, 2016

Christopher Vogt (cvogt) said:
Another workaround: {code}List("ab").to[Set].head.head{code}

@joroKr21
Copy link
Member

Interesting comparison with typelevel scala:

  • 2.12.4-bin-typelevel-4
scala> List("ab").toSet.head.head
<console>:12: error: value head is not a member of type parameter B
       List("ab").toSet.head.head

scala> List("ab").toSet.map(_.length)
res4: scala.collection.immutable.Set[Int] = Set(2)
  • 2.12.6
scala> List("ab").toSet.head.head
<console>:12: error: value head is not a member of type parameter B
       List("ab").toSet.head.head
                             ^

scala> List("ab").toSet.map(_.length)
<console>:12: error: missing parameter type for expanded function ((x$1: <error>) => x$1.length)
       List("ab").toSet.map(_.length)

@milessabin any idea what causes the second example to compile on typelevel scala? Might be worth porting over.

@milessabin
Copy link

milessabin commented Sep 26, 2018

@joroKr21 I'm pretty sure it's down to some experimental bounds propagation work that @paulp did (scala/scala@dc1bbb9) which turned out to be important to the literal types PR.

I'm not quite sure where it ended up. I rolled it into the the unconditionalizing of literal types (scala/scala@beb2dac), then it might have been removed, or at least simplified by @adriaanm (scala/scala@272d17b).

How does it look in 2.13.0-M5?

@joroKr21
Copy link
Member

  • 2.13.0-20180923-122040-2e82444 (currently latest)
scala> List("ab").toSet.head.head
                             ^
       error: value head is not a member of type parameter B

scala> List("ab").toSet.map(_.length)
                            ^
       error: missing parameter type for expanded function ((x$1: <error>) => x$1.length)

But looking at the tests added in scala/scala@272d17b this appears to be intentional.

@milessabin
Copy link

milessabin commented Sep 26, 2018

I think the tests might be documenting the status quo, however. It would be good to know what breakage was actually motivating sticking with that. @adriaanm?

It might be worth reviving this as a separate PR. I'd certainly consider it for the next TLS releases.

@adriaanm
Copy link
Contributor

There's a second test to illustrate the problem with adding the bounds from the start. You end up over-constraining the type variable. Sometimes it's important that we know it's undetermined (by the environment), so that we use Wildcard as the expected type, rather than the bound that's on the type parameter. Typing without expected type may result in a sharper type than typing under, say, AnyVal.

@SethTisue SethTisue added this to the Backlog milestone Oct 19, 2018
@SethTisue SethTisue added infer fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) labels Oct 20, 2020
@SethTisue
Copy link
Member

Hasn't progressed as of 2.13.3, but is fixed in Dotty.

@SethTisue
Copy link
Member

says @smarter on Gitter:

basically scala 2 instantiate type variables as soon as it sees that they have a lower-bound different from Nothing, whereas dotty waits until the last possible moment to do so. this will never be fixed in scala 2.

@som-snytt
Copy link

Closing as not progressing in Scala 2. Leaving it open is not documentary, but it ought to be a FAQ.

@som-snytt som-snytt closed this as not planned Won't fix, can't repro, duplicate, stale Nov 14, 2023
@SethTisue SethTisue removed this from the Backlog milestone Nov 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) infer typer
Projects
None yet
Development

No branches or pull requests

6 participants