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

时间:2018-11-26 20:46:58

标签: scala partialfunction

我需要完成以下代码段才能完成作业。为了完成任务,我必须正确替换注释/*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"
    }
}

1 个答案:

答案 0 :(得分:3)

您编写了Failures: 1) HousesController GET #index assigns @houses Failure/Error: expect(assigns(:houses).results).to eq([h]) expected: [#<House id: 763, rent: 1173, deposit: 739, description: "Rerum cado curso curo alias.", preferred_ge...2018-11-26 21:40:43", available_at: "2018-12-17", user_id: 15945, lease_length: nil, built_in: nil>] got: [#<House id: 215, rent: 0.839e3, deposit: 0.797e3, description: "Rerum aeneus taceo crepusculum aestu...2018-11-26 21:17:53", available_at: "2018-12-17", user_id: 15776, lease_length: nil, built_in: nil>] (compared using ==) Diff: @@ -1,2 +1,5 @@ -[#<House id: 763, rent: 1173, deposit: 739, description: "Rerum cado curso curo alias.", preferred_gender: 0, created_at: "2018-11-26 21:40:43", updated_at: "2018-11-26 21:40:43", available_at: "2018-12-17", user_id: 15945, lease_length: nil, built_in: nil>] +[#<House id: 215, rent: 0.839e3, deposit: 0.797e3, description: "Rerum aeneus taceo crepusculum aestus.", preferred_gender: 0, created_at: "2018-11-25 12:50:11", updated_at: "2018-11-25 12:50:11", available_at: "2018-12-16", user_id: 8065, lease_length: nil, built_in: nil>, + #<House id: 235, rent: 0.519e3, deposit: 0.642e3, description: "Cicuta totidem arbustum arcesso fugit tego.", preferred_gender: 0, created_at: "2018-11-25 12:54:28", updated_at: "2018-11-25 12:54:28", available_at: "2018-12-16", user_id: 8085, lease_length: nil, built_in: nil>, + #<House id: 648, rent: 0.668e3, deposit: 0.1104e4, description: "Corporis tametsi demens.", preferred_gender: 0, created_at: "2018-11-26 21:17:43", updated_at: "2018-11-26 21:17:43", available_at: "2018-12-17", user_id: 15775, lease_length: nil, built_in: nil>, + #<House id: 649, rent: 0.799e3, deposit: 0.611e3, description: "Ut ancilla tredecim.", preferred_gender: 0, created_at: "2018-11-26 21:17:53", updated_at: "2018-11-26 21:17:53", available_at: "2018-12-17", user_id: 15776, lease_length: nil, built_in: nil>] # ./spec/controllers/houses_controller_spec.rb:12:in `block (3 levels) in <top (required)>' 函数,该函数返回require的{​​{1}}接口。 您应该用类似这样的内容重写它:

T => Option[Boolean]