Generics Kotlin-如何实现具有高阶函数作为泛型参数的类

Generics Kotlin-如何实现具有高阶函数作为泛型参数的类,generics,kotlin,higher-order-functions,Generics,Kotlin,Higher Order Functions,我想实现一个泛型类,它将任意算术函数作为泛型参数类型 我有一个框架实现: abstract class Action<T> { internal abstract val performable: ObservableBooleanValue internal abstract val perform: T } 这可能吗? 我查阅了一些文档,发现了一个function n接口,也许我可以用它来创建我的类Action,但是我该如何实例化我的对象呢?因为object:A

我想实现一个泛型类,它将任意算术函数作为泛型参数类型

我有一个框架实现:

abstract class Action<T> {
    internal abstract val performable: ObservableBooleanValue
    internal abstract val perform: T
}
这可能吗?
我查阅了一些文档,发现了一个
function n
接口,也许我可以用它来创建我的类
Action
,但是我该如何实例化我的对象呢?因为
object:Action Unit>
使编译器抱怨Kotlin至少具有以下在这种情况下似乎有用的功能:

  • Kotlin对元组有一些支持(
    Pair
    s,
    Triple
    s,…)
  • Kotlin为接受元组的lambda提供了很好的解构语法
  • 有一个
    Unit
    -类型将
    Unit
    -值作为唯一的值:这允许抽象扩展到不需要参数的情况(
    Unit
    在某种意义上是“零元”元组)
  • 总之,这些特性允许您对将任意类型的
    T
    作为单个参数的回调进行抽象,并返回一个
    单元

    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")
            }
        }
    
    }
    
    接口可观测值{
    fun get():布尔值
    }
    数据类SimpleBoleAnProperty(值:布尔):ObservableBoleAnValue{
    重写fun get():Boolean=值
    }
    数据类点(val x:Int,val y:Int)
    数据类颜色(val为黑色:布尔)//否则为白色。
    抽象集体诉讼{
    可执行的内部抽象值:ObservableBoleAnValue
    内部抽象值执行:(T)->单位
    操作员乐趣调用(设置:T)=
    如果(performable.get())执行(设置)
    else println(“无法执行”)
    }
    反对到别的地方去{
    private val install=object:Action(){
    override val可执行:ObservableBoleAnValue=SimpleBoleAnProperty(真)
    覆盖val执行:(三重)->单位=
    {(颜色、形状、点)->
    println(“颜色:$Color,形状:$Shape,Point:$Point”)
    }
    }
    private val delete=object:Action(){
    override val可执行:ObservableBoleAnValue=SimpleBoleAnProperty(真)
    覆盖val perform={{单元->
    println(“删除”)
    }
    }
    }
    

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

    当您不知道算术时,如何传递要调用的参数?(你能取而代之的是玩一个包含一系列参数的游戏吗??)
    abstract class Action<T> {
        ...
        operator fun invoke(???) = 
                if (performable.get()) perform(???) 
                else println("Cannot Perform")
    }
    
    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")
            }
        }
    
    }