I am trying a filesearcher program in scala.
FileChecker.scala
package fileSearcher import sun.org.mozilla.javascript.internal.ast.Yield class FilterChecker(filter:String) { def matches(content: String) = content contains filter def findMatchedFiles(fileObjects: List[IOObject]) = for(fileObject <- fileObjects if(fileObject.isInstanceOf[FileObject]) if(matches(fileObject.name))) yield fileObject } object FilterChecker{ def apply(filter: String)=new FilterChecker(filter) } IOObject.scala
package fileSearcher import java.io.File trait IOObject { val file:File val name= file.getName() } case class FileObject(file: File)extends IOObject case class DirectoryObject(file: File)extends IOObject FileConverter.scala
package fileSearcher import java.io.File object FileConverter { def convertToIOObject(file: File){ if(file.isDirectory()) DirectoryObject(file) else FileObject(file) } } Matcher.scala
package fileSearcher import java.io.File class Matcher(filter:String,rootLocation:String) { val rootIOObject=FileConverter.convertToIOObject(new File(rootLocation)) def execute()={ val matchedFiles= rootIOObject match { case file : FileObject if FilterChecker(filter) matches file.name =>List(file) //error 1 case directory: DirectoryObject => ??? //error 2 case _ => List() } matchedFiles map(iOObject => iOObject.name) } } The above code gives me 2 error.
scrutinee is incompatible with pattern type; found : fileSearcher.FileObject required: Unit scrutinee is incompatible with pattern type; found : fileSearcher.DirectoryObject required: Unit Can anybody explain me what I am doing wrong and how can I resolve it?
Thanks
31 Answer
You are missing an equals sign:
def convertToIOObject(file: File) = { ^ here Without it, the compiler assumes that the method returns Unit, which is why the match statement was confused: it can't be a FileObject or DirectoryObject if it has to be a Unit.