سؤال

I am learning Scala with Scala-IDE in Eclipse. While following the Chapter 9: Control Abstraction, part 1: Reducing Code Duplication in the Programming in Scala book, I have written the code from the book (fully represented below), it worked fine! When I started removing the unnecessary blank lines, a strange thing happened. Here is the entire code before the removal of the blank lines in the filterFiles() method:

object Code_c09s01_ControlAbstraction extends App{

  object FilesFilter {

    private def filterFiles(path: String, pattern: String, matcher: (String, String) => Boolean) = {

      val files = (new java.io.File(path)) listFiles

      for(file <- files if matcher(file.getName, pattern)) yield file
    } 

    def filterExtension(path: String, pattern: String) = filterFiles(path, pattern, _.endsWith(_))
    def filterName(path: String, pattern: String) = filterFiles(path, pattern, _.contains(_))
    def filterRegex(path: String, pattern: String) = filterFiles(path, pattern, _.matches(_))
  }

  def printArray[A](message: String, arr: Array[A]) {
    println (message)
    println (arr mkString("\n"))
  }

  def test() {
    val path = "C:\\";

    printArray("--- filtering by ext: ---", FilesFilter.filterExtension(path, ".txt"))
    printArray("--- filtering by containment: ---", FilesFilter.filterName(path, "1"))
    printArray("--- filtering by regex: ---", FilesFilter.filterRegex(path, "."))
  }

  test

}

which works just fine! However, after removing the blank lines from the filterFiles() method, the method now looks like this:

private def filterFiles(path: String, pattern: String, matcher: (String, String) => Boolean) = {
  val files = (new java.io.File(path)) listFiles
  for(file <- files if matcher(file.getName, pattern)) yield file
} 

And the IDE gives me errors on both lines of the body. The error of the first line says:

ambiguous reference to overloaded definition, both method listFiles in class File of type (x$1: java.io.FileFilter)Array[java.io.File] and method listFiles in class File of type 
 (x$1: java.io.FilenameFilter)Array[java.io.File] match argument types (Null)

the error on the second line says:

illegal start of simple expression

and all the three calls to the printArray() in the test() method now also tell this:

type mismatch; found : Unit required: Array[?]

What does it all mean? Scala is not supposed to behave like Python when code alignment can ruin the code flow... so how come that removing the blank line between the first and the second line of the body of the filterFiles() method puts up such a serious error? Is it a bug somewhere or does it follow directly from the rules of Scala? Note: if I add ; between the line, it sorts everything out. Is it just semicolon inference bug?

هل كانت مفيدة؟

المحلول

When object's method can none or single argument you can call it like

val files = object method arg

or, in your snippet

val files = object method
  arg

In your code compiler wants to call listfiles with an for expression as an agument which returns Array[File]. And complains that listFiles hasn't implementation with this type of argument. Empty line prevents treating for expression as a parameter for listFiles function

نصائح أخرى

Looks like semicolon inference problem as you suggest, I guess the compiler expects method arguments if you leave parenthesis out. Instead of semicolon, you can add parenthesis after listFiles:

val files = (new java.io.File(path)) listFiles ()
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top