简体   繁体   中英

Kotlin - How to implement a Class that has a Higher Order Function as a generic parameter

I want to implement a generic class that takes as a generic parameter type a function of arbitrary arity.

I have a skeletal implementation:

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

and a couple of objects:

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")
}

I will be having a couple more of these objects and would like to make the invoke function a member of an Action class so that I don't have to implement it for each object. I want to achieve something like this:

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

Is that even possible? I've looked through some documentation and found a FunctionN<out R> interface, maybe I could use it to make my class Action<T: FunctionN<Unit>> but then how do I instantiate my objects? Because object: Action<() -> Unit> makes the compiler complain

Kotlin has at least the following features that seem helpful in this case:

  1. Kotlin has some support for tuples ( Pair s, Triple s, ...)
  2. Kotlin has nice destructuring syntax for lambdas that take tuples
  3. There is a Unit -type that has Unit -value as the only value: this allows the abstraction to extend to the cases where no parameters are needed ( Unit is the "zero-ary" tuple, in a sense)

Together, these features allow you to abstract over callbacks that take an arbitrary type T as a single parameter, and return a 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")
        }
    }

}

It at least compiles. I have no reason not to assume that it would run just as nicely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM