Skip to content

type error of val in while condition #12750

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

Closed
Centaur opened this issue Jun 8, 2021 · 2 comments
Closed

type error of val in while condition #12750

Centaur opened this issue Jun 8, 2021 · 2 comments

Comments

@Centaur
Copy link

Centaur commented Jun 8, 2021

Compiler version

3.0.0 , 3.0.1-RC1

Minimized code

  val c = List(1,2,3)
  var these = c
  while 
    val ne:Option[List[Int]] = Some(these.map(_+1))
    ne.get.nonEmpty
  do
    these = ne.map(_.tail).get

Output

value map is not a member of Object => Boolean

Expectation

should compile successfully

@bishabosha
Copy link
Member

bishabosha commented Jun 8, 2021

This is an example of shadowing of the pre-existing ne method on AnyRef, if we rename val ne to val newList then the error becomes more clear:

val c = List(1,2,3)
var these = c
while 
  val newList:Option[List[Int]] = Some(these.map(_+1))
  newList.get.nonEmpty
do
  these = newList.map(_.tail).get

we get the error:

7 |    these = newList.map(_.tail).get
  |            ^^^^^^^
  |            Not found: newList

This is because the definitions in the block of the condition of the while loop are not visible to the block in the do part

@som-snytt
Copy link
Contributor

som-snytt commented Jun 8, 2021

The misleading error was fixed in Scala 2 under scala/bug#5389

where ne is not due to wrapping object but member of Predef.

✗ scalac -d /tmp test/files/neg/t5389.scala
-- [E083] Type Error: test/files/neg/t5389.scala:2:7 ----------------------------------------------------------------------------
2 |import ne.scala
  |       ^^
  |       Object => Boolean is not a valid import prefix, since it is not an immutable path

Edit: forgot I'd created a ticket, pre-pandemic was a decade ago #7713

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

3 participants