1

I am doing programming in scala for looping through map.

Below is my code which works fine.

     val names = Map("fname" -> "Robert", "lname" -> "Goren")

     for((k,v) <- names ) println(s"Key: $k, Value : $v")

When looping through the map, if I give (K,V) instead of (k,v), the program is not compiling. It gives cannot resolve symbol error.

Below is my for loop -

     for((K,V) <- names ) println(s"Key: $K, Value : $V")

I am executing this program in IntelliJ IDEA 15 scala worksheet.

Can anyone please explain the reason for this error.

user51
  • 8,843
  • 21
  • 79
  • 158

1 Answers1

5

It doesn't compile for the same reason this code doesn't compile:

val (A,B) = (1,2)
// error: not found: value A
// error: not found: value B

but this does compile:

val (a,b) = (1,2)
// a: Int = 1
// b: Int = 2

Constant names should be in upper camel case. That is, if the member is final, immutable and it belongs to a package object or an object, it may be considered a constant

Method, Value and variable names should be in lower camel case

Source: http://docs.scala-lang.org/style/naming-conventions.html

Community
  • 1
  • 1
Filippo Vitale
  • 7,597
  • 3
  • 58
  • 64
  • some more explanation here: http://stackoverflow.com/questions/12636972/how-to-pattern-match-into-an-uppercase-variable – Łukasz Jan 16 '16 at 10:27