7

I've seen this kind of code on countless websites yet it doesn't seem to compile:

def foo(): (Int, Int) = {
        (1, 2)
}

def main(args: Array[String]): Unit = {
        val (I, O) = foo()
}

It fails on the marked line, reporting:

  • not found: value I
  • not found: value O

What might be the cause of this?

1

2 Answers 2

10

The problem is the use of upper case letters I and O in your pattern match. You should try to replace it by lowercase letters val (i, o) = foo(). The Scala Language Specification states that a value definition can be expanded to a pattern match. For example the definition val x :: xs = mylist expands to the following (cf. p. 39):

val x$ = mylist match { case x :: xs => {x, xs} }
val x = x$._1
val xs = x$._2

In your case, the value definition val (i, o) = foo() is expanded in a similar way. However, the language specification also states, that a pattern match contains lower case letters (cf. p. 114):

A variable pattern x is a simple identifier which starts with a lower case letter.

1
  • spent 2 hours looking for this answer. thanks Dec 19, 2022 at 15:12
3

As per Scala naming convention,

Method, Value and variable names should be in camelCase with the first letter lower-case:

Your I, O are pattern variables. However, you have to use caution when defining them. By convention, Scala expects the pattern variables to start with a lowercase letter and expects constants to start with an uppercase letter. So, the code will not compile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.