Class 构造函数要求 'customPredicate: Message.() -> Boolean' 我不明白这是什么意思

Class constructor asks for 'customPredicate: Message.() -> Boolean' and I cant figure out what is it mean

我有以下界面

interface Filter {
    fun checkFor(message: Message): Boolean = message.predicate()
    fun Message.predicate(): Boolean

infix fun and(otherFilter: Filter): Filter = object : Filter {
    override fun Message.predicate(): Boolean =
        this@Filter.checkFor(this) && otherFilter.checkFor(this)
}

infix fun or(otherFilter: Filter): Filter = object : Filter {
    override fun Message.predicate(): Boolean =
        this@Filter.checkFor(this) || otherFilter.checkFor(this)
}

operator fun not(): Filter = object : Filter {
    override fun Message.predicate(): Boolean = !this@Filter.checkFor(this)
}

class Custom(private val customPredicate: Message.() -> Boolean) : Filter {
    override fun Message.predicate(): Boolean = customPredicate()
}

object All : Filter {
    override fun Message.predicate(): Boolean = true
}

在这个界面中,我有一个 class 名为“Custom” 在构造函数中询问

class Custom(private val customPredicate: Message.() -> Boolean)

而且我不知道我应该如何使用这个 class 来创建我自己的过滤器

请协助

创建 Custom class 对象的方法有很多种。

您可以将 lambda 表达式用作接收器的函数,接收器在正文中可用,如 context

val custom = Custom { //lambda begins here
    //here you can call any method of Message, which is the receiver
}

您可以创建 Message 的扩展函数并将其引用传递给 Custom class 构造函数。

fun Message.customPredicate() {
    // Your code here
}

将此函数的引用传递给 Custom class

constructor
val custom = Custom(Message::customPredicate)

您还可以使用匿名函数创建 Custom class 的 object

val custom = Custom(fun Message.() {
    // Your code here
})