受检者与模式类型不兼容;发现:package.SomeObject 需要:单位
scrutinee is incompatible with pattern type; found : package.SomeObject required: Unit
我正在尝试使用 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)
}
}
上面的代码给出了 2 个错误。
scrutinee is incompatible with pattern type; found : fileSearcher.FileObject required: Unit
scrutinee is incompatible with pattern type; found : fileSearcher.DirectoryObject required: Unit
谁能解释一下我做错了什么,我该如何解决?
谢谢
您缺少一个等号:
def convertToIOObject(file: File) = {
^
here
没有它,编译器假定方法 returns Unit
,这就是 match
语句被混淆的原因:它不能是 FileObject
或DirectoryObject
如果必须是 Unit
.
我正在尝试使用 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)
}
}
上面的代码给出了 2 个错误。
scrutinee is incompatible with pattern type; found : fileSearcher.FileObject required: Unit
scrutinee is incompatible with pattern type; found : fileSearcher.DirectoryObject required: Unit
谁能解释一下我做错了什么,我该如何解决?
谢谢
您缺少一个等号:
def convertToIOObject(file: File) = {
^
here
没有它,编译器假定方法 returns Unit
,这就是 match
语句被混淆的原因:它不能是 FileObject
或DirectoryObject
如果必须是 Unit
.