5

In Scala 2.13 I have encountered a problem with pattern matching using the operator #::, which displays error Cannot resolve method #::.unapply when used as follows:

def exampleFunction(lazyList: LazyList[Int]):Unit =
  lazyList match {
    case LazyList() => println("End")
    case head #:: tail => println(head); exampleFunction(tail) // Cannot resolve method #::.unapply
  }
exampleFunction(LazyList(1,2,3,4,5,6,7,8))

When the LazyList is generic, the operator does work as expected:

def exampleFunction[A](lazyList: LazyList[A]):Unit =
  lazyList match {
    case LazyList() => println("End")
    case head #:: tail => println(head); exampleFunction(tail)
  }
exampleFunction(LazyList(1,2,3,4,5,6,7,8)) // output: 1 2 3 4 5 6 7 8 End

Why does this problem occur and is there a way to fix it?

kamov
  • 83
  • 2
  • 6
  • 3
    Your first snippet version appears to compile fine, see https://scastie.scala-lang.org/iab8q5EdS0qV20xLwY4J0Q – Marth Nov 24 '19 at 21:39
  • 3
    I cannot seem to replicate the issue on my machine. Try with `import scala.collection.immutable.LazyList.#::` – Mario Galic Nov 24 '19 at 22:05
  • 2
    Adding `import scala.collection.immutable.LazyList.#::` fixed the problem completely and everything now works, thank you! – kamov Nov 24 '19 at 22:19
  • @kamnov . I also faced the same issue and adding the import solved the issue. How ever I am not sure why we need to add the import explicitly in case of LazyList, when this is not applicable in case of List. Any answer will be appreciated. – Shantiswarup Tunga Apr 27 '20 at 21:02
  • @MarioGalic you should put an answer with your comment, it also fixed this issue for me. I also don't understand why this is needed. – jrod May 03 '20 at 15:20
  • @jrod Are you using IntelliJ? – Mario Galic May 03 '20 at 15:24
  • @MarioGalic yes, IntelliJ IDEA 2019.1.3 Community edition. – jrod May 08 '20 at 16:25

1 Answers1

3

In case you are using IntelliJ, this might be due to in-editor error highlighting bug SCL-15834: Highlighting error on case matching using operator #:: In other words, this is a false positive where the code compiles successfully however IntelliJ's custom error highlighting process incorrectly identifies a problem. Providing explicit import

import scala.collection.immutable.LazyList.#::

seems to make the editor error highlighting happy, however the import should not be necessary. Few other suggestion to try

  • File | Invlidate Caches
  • From the root of the project rm -fr .idea and then re-import project
  • Update to bleeding edge Scala Plugin version: Preferences | Languages & Frameworks | Scala | Updates | Update channel | Nightly Builds
  • Within Registry... enable experimental flag scala.highlighting.compiler.errors.in.editor
Mario Galic
  • 47,285
  • 6
  • 56
  • 98