Found : Int(0) required: Int

Can anyone please help me with this error Please - getting this error in IntelliJ using Scala 2.12
Is this a Scala Version issue ?

However works on Ammonite repl below
Welcome to the Ammonite Repl 2.5.0 (Scala 2.13.7 Java 11.0.9)

def runningSum[Int] (l1: List[Int]): List[Int] = {
import scala.collection.mutable.ListBuffer
val l = new ListBufferInt
var s1:Int = 0

l1.foreach(x => {
  s1 += x
  l.append(s1)
})
l.toList

}

***Error on line - var s1:Int = 0

type mismatch;
found : Int(0)
required: Int
var s1:Int = 0

The problem is that you’re introducing a type variable called Int when you say def runningSum[Int](...) so you have both a type variable called Int and the built-in type Int (the type of 0) which are not the same! Very confusing. Took me a minute to see it. In any case remove the type parameter and it will work: def runningSum(l1: List[Int]): List[Int] = ...

1 Like

Excellent advice thank u so much - yes it worked
:slight_smile:

Note that in Scala 3 you get a much better error message. It distinguishes the two kinds of Int and explains where they come from.

scala> def runningSum[Int] (l1: List[Int]): List[Int] = List(1)
-- Error:
1 |def runningSum[Int] (l1: List[Int]): List[Int] = List(1)
  |                                                      ^
  |                             Found:    (1 : Int)
  |                             Required: Int²
  |
  |                             where:    Int  is a class in package scala
  |                                       Int² is a type in method runningSum
1 Like

tpolecat forgot to say “use sbt-tpolecat or at least always enable -Xlint”, which is too long for a trucker cap but could work on a t-shirt:

➜  ~ scala -Xlint
Welcome to Scala 2.13.8 (OpenJDK 64-Bit Server VM, Java 17.0.1).
Type in expressions for evaluation. Or try :help.

scala> def f[Int] = ???
             ^
       warning: type parameter Int defined in method f shadows class Int defined in package scala. You may want to rename your type parameter, or possibly remove it.
def f[Int]: Nothing
1 Like