Scrutinee Is Incompatible with Pattern Type; Found: Package. Someobject Required: Unit

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

3

1 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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest