Kabeer Ahmed
Kabeer Ahmed

Reputation: 139

Unapplied methods are only converted to functions when a function type is expected

The below piece of code does work directly without issues in Spark Shell with Scala 2.11.12, on the Scastie website. But in Jupyter Notebook running Apache Toree, there is a weird error.

// polymorphic functions

def findFirst[A](as: List[A], p: A => Boolean): Int = {

  val listLength = as.length

  @annotation.tailrec
  def go(n: Int): Int = {

    if (n >= listLength) -1
    else if (p(as(n))) n
    else go(n + 1)

  }

  go(0)

}

def passFun(x: Int): Boolean = x == 2

val l: List[Int] = List(1, 2, 3)
// println("The findFirst of function is: ")
println(s"Answer is: ${findFirst(l, passFun)}")

There is no error when this code is first executed after a kernel is just started. But immediately when the same cell is executed, the below error is shown:

The Answer is: 1 a = 1 :28: error: missing argument list for method findFirst Unapplied methods are only converted to functions when a function type is expected. You can make this conversion explicit by writing findFirst _ or findFirst(_,_) instead of findFirst. findFirst ^ lastException: Throwable = null findFirst: [A](as: List[A], p: A => Boolean)Int 1

The error is gone when the kernels are restarted. Can someone please let me know if they know the solution or the cause of this issue?

Upvotes: 2

Views: 1139

Answers (0)

Related Questions