Skip to content

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

Closed as not planned
Closed as not planned

Description

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

Activity

  1. scabug commented on Aug 12, 2013

    @scabug
    Author

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

  2. scabug commented on Aug 12, 2013

    @scabug
    Author

    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
  3. scabug commented on Aug 12, 2013

    @scabug
    Author

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

  4. scabug commented on Aug 12, 2013

    @scabug
    Author

    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
  5. scabug commented on Aug 12, 2013

    @scabug
    Author

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

  6. scabug commented on Dec 5, 2013

    @scabug
    Author

    @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

  7. scabug commented on Aug 9, 2016

    @scabug
    Author

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

  8. joroKr21 commented on Sep 26, 2018

    @joroKr21
    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.

  9. milessabin commented on Sep 26, 2018

    @milessabin

    @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?

  10. joroKr21 commented on Sep 26, 2018

    @joroKr21
    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.

  11. milessabin commented on Sep 26, 2018

    @milessabin

    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.

  12. adriaanm commented on Sep 26, 2018

    @adriaanm
    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.

  13. added this to the Backlog milestone on Oct 19, 2018
  14. added
    fixed in Scala 3This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/)
    on Oct 20, 2020
  15. SethTisue commented on Oct 20, 2020

    @SethTisue
    Member

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

  16. SethTisue commented on Oct 21, 2020

    @SethTisue
    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.

  17. som-snytt commented on Nov 14, 2023

    @som-snytt

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

  18. removed this from the Backlog milestone on Nov 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    fixed in Scala 3This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/)infertyper

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions