Why this value get cleared?

scala> val li = Source.fromFile(file).getLines
                                      ^
       warning: Auto-application to `()` is deprecated. Supply the empty argument list `()` explicitly to invoke method getLines,
       or remove the empty argument list from its definition (Java-defined methods are exempt).
       In Scala 3, an unapplied method like this will be eta-expanded into a function.
val li: Iterator[String] = <iterator>

scala> val li2 = li.toList
val li2: List[String] = List([email protected], ...

scala> for (x<- li) println(x)
(null)


my questions are:

  1. warning: Auto-application to () is deprecated. what does this mean? how to remove it?
  2. why the value li get cleared after it was assigned to li2?

Thank you a lot.

  1. The correct code is val li = Source.fromFile(file).getLines(), i.e., adding empty parenthesis at the end. Adding this by the compiler is the auto-application in the warning.

  2. The .getLines returns an Iterator which is a collection that is to be read just once. During creation of the list li2 the iterator is read, so becomes empty.

Siddhartha

3 Likes