Scala 缺少扩展函数的参数类型匿名函数的参数类型必须是完全已知的。 (SLS 8.5)

Scala missing parameter type for expanded function The argument types of an anonymous function must be fully known. (SLS 8.5)

我需要完成以下代码片段才能完成作业。为了完成任务,我必须正确替换注释 /*fulfill ...*/。但是我尽了最大努力,我仍然得到

缺少扩展函数的参数类型匿名函数的参数类型必须是完全已知的。 (SLS 8.5) 错误。

我发现了与此错误相关的类似问题。但是,我无法针对这些答案的特殊问题得出解决方案。

所以目标是检查事件是否满足属性。

我很高兴得到每一个提示。

这是我需要完成的代码:

import scala.collection.mutable.MutableList

abstract class Event
case class Command(cmdName: String) extends Event
case class Succeed(cmdName: String) extends Event
case class Fail(cmdName: String) extends Event

class Property(val name: String, val func: () => Boolean)

class Monitor[T] {
    val properties = MutableList.empty[Property]

    // (propName: String)(formula: => Boolean) was inserted by me
    def property(propName: String)(formula: => Boolean) /* fulfill declaration here */ {
        properties += new Property(propName, formula _)
    }

    var eventsToBeProcessed = List[T]()

    def check(events: List[T]) {
        for (prop <- properties) {
            eventsToBeProcessed = events

            println(prop.func())                
        }
    }

    def require(func: PartialFunction[T, Boolean]):Boolean = {
        /* Fulfill body */

        // This is what I came up with and what throws the compilation error
        // case event:T => if (func.isDefinedAt(event)) Some(func(event)) else None
        // Edit 1: Now I tried this but it prints that properties evaluate to false
        var result = true
        for (event <- eventsToBeProcessed){
            if (func.isDefinedAt(event)){
                result = result && func(event)
            }
        }
        return  result
    }
}

class EventMonitor extends Monitor[Event] {
    property("The first command should succeed or fail before it is received again") {
        require {
            case Command(c) =>
                require {
                    case Succeed(`c`) => true
                    case Fail(`c`) => true
                    case Command(`c`) => false
                }
        }
    }

    property("The first command should not get two results") {
        require {
            case Succeed(c) =>
                require {
                    case Succeed(`c`) => false
                    case Fail(`c`) => false
                    case Command(`c`) => true
                }
            case Fail(c) =>
                require {
                    case Succeed(`c`) => false
                    case Fail(`c`) => false
                    case Command(`c`) => true
                }
        }
    }

    property("The first command should succeed") {
        /* Add a property definition here which requires that the first command does not fail.
         * It should yield OK with the events listed in the main method.
         */

        // This is what I came up with
        require{
            case Command(c) =>
                require{
                    case Succeed(`c`)=> true
                    case Fail(`c`) => false
                }
        }
    }
}

object Checker {

    def main(args: Array[String]) {
        val events = List(
            Command("take_picture"),
            Command("get_position"),
            Succeed("take_picture"),
            Fail("take_picture")
        )

        val monitor = new EventMonitor
        monitor.check(events)
        // Desired output should be "true false true"
    }
}

您编写了 require 函数 returns T => Option[Boolean] 而不是 Boolean。 你应该像这样重写它:

def require(func: PartialFunction[T, Boolean]):Boolean = {
  val left = eventsToBeProcessed.dropWhile(!func.isDefinedAt(_))
  left.headOption.forall(head => {
    eventsToBeProcessed = left.tail
    func(head)
  })
}