【问题标题】:Kotlin - How to implement a Class that has a Higher Order Function as a generic parameterKotlin - 如何实现具有高阶函数作为泛型参数的类
【发布时间】:2019-08-30 12:06:08
【问题描述】:

我想实现一个泛型类,它将任意数量的函数作为泛型参数类型。

我有一个骨架实现:

abstract class Action<T> {
    internal abstract val performable: ObservableBooleanValue
    internal abstract val perform: T
}

还有几个对象:

private val install = object : Action<(Int, Int, Point) -> Unit>() {
    override val performable: ObservableBooleanValue = SimpleBooleanProperty(true)
    override val perform = { color: Int, shape: Int, point: Point ->
        println("Color: $color, Shape: $shape, Point: $point")
    }

    operator fun invoke(color: Int, shape: Int, point: Point) =
            if (performable.get()) perform(color, shape, point)
            else println("Cannot Perform")
}


private val delete = object : Action<() -> Unit>() {
    override val performable: ObservableBooleanValue = SimpleBooleanProperty(true)
    override val perform = {
        println("Deleting")
    }

    operator fun invoke() =
            if (performable.get()) perform()
            else println("Cannot Perform")
}

我将拥有更多这样的对象,并希望将invoke 函数作为Action 类的成员,这样我就不必为每个对象都实现它。 我想实现这样的目标:

abstract class Action<T> {
    ...
    operator fun invoke(???) = 
            if (performable.get()) perform(???) 
            else println("Cannot Perform")
}

这可能吗? 我查看了一些文档并找到了一个FunctionN&lt;out R&gt; 接口,也许我可以用它来创建我的类Action&lt;T: FunctionN&lt;Unit&gt;&gt; 但是我如何实例化我的对象?因为object: Action&lt;() -&gt; Unit&gt; 让编译器抱怨

【问题讨论】:

  • 当你不知道arity时,你将如何传递参数来调用? (你能不能有一个有趣的参数数组??)

标签: generics kotlin higher-order-functions


【解决方案1】:

Kotlin 至少具有以下在这种情况下看起来很有帮助的功能:

  1. Kotlin 对元组有一些支持(Pairs、Triples、...)
  2. Kotlin 为采用元组的 lambda 提供了很好的解构语法
  3. 有一个Unit-type 具有Unit-value 作为唯一值:这允许抽象扩展到不需要参数的情况(Unit 是“零元组”元组,某种意义上)

这些特性让您可以抽象出将任意类型 T 作为单个参数的回调,并返回 Unit

interface ObservableBooleanValue {
  fun get(): Boolean
}

data class SimpleBooleanProperty(val value: Boolean) : ObservableBooleanValue {
  override fun get(): Boolean = value
}

data class Point(val x: Int, val y: Int)
data class Color(val isBlack: Boolean) // otherwise white.

abstract class Action<T> {
    internal abstract val performable: ObservableBooleanValue
    internal abstract val perform: (T) -> Unit

    operator fun invoke(settings: T) = 
        if (performable.get()) perform(settings) 
        else println("Cannot Perform")
}

object SomewhereElse {

    private val install = object : Action<Triple<Int, Int, Point>>() {
        override val performable: ObservableBooleanValue = SimpleBooleanProperty(true)
        override val perform: (Triple<Int, Int, Point>) -> Unit = 
            { (color, shape, point) ->
                println("Color: $color, Shape: $shape, Point: $point")
            }
    }


    private val delete = object : Action<Unit>() {
        override val performable: ObservableBooleanValue = SimpleBooleanProperty(true)
        override val perform = { _: Unit ->
            println("Deleting")
        }
    }

}

它至少可以编译。我没有理由不认为它会运行得一样好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-11
    • 2011-06-15
    • 1970-01-01
    • 1970-01-01
    • 2018-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多