generics - Kotlin 接口(interface)函数可互换参数

标签 generics kotlin interface

我目前在一个接口(interface)上工作,它有一个简单的功能,所有扩展这个接口(interface)的类都应该实现。 但是这些类应该可以像这样用不同的函数参数调用

interface IDoSomething<T> {

    fun execute(vararg any: Any?): T // Make function arguments interchangeable

}
class DoSomethingA : IDoSomething<String> {

    // This is what i want
    override fun execute(int: Int, boolean: Boolean): String {
        println("Do something with parameters: $int, $boolean")
        ...  
    }

    // This is what i need to do
    override fun execute(vararg any: Any?): String {
        val int = any[0] as Int
        val boolean = any[1] as Boolean
        println("Do something with parameters: $int, $boolean")
        ...
    }
}

实现此接口(interface)的其他类应该可以有其他参数

class DoSomethingB : IDoSomething<String> {

    // Should also be possible with same interface
    override fun execute(string: String, double: Double, boolean: Boolean): String {
        println("Do something with parameters: $string, $double, $boolean")
        ...
    }

}

Kotlin 中有什么可以帮助我做这样的事情吗?或者存在一种有助于解决此类问题的模式。

最佳答案

语言中没有内置任何东西来实现您想要的(例如 C++ 可变参数模板)。

但是您仍然可以使用通用输入并用包装它们的类替换多个参数来实现您想要的:

interface IDoSomething<I, T> {

  fun execute(input: I): T
}

class DoSomethingB : IDoSomething<Pair<String, Double>, String> {

  // Should also be possible with same interface
  override fun execute(input: Pair<String, Double>): String {
    val (string, double) = input
    println("Do something with parameters: $string, $double")
    ...
  }
}

这是对您的问题最简单的解决方案。

你有另一个更复杂的解决方案。

你可以有一个注解(例如@Input),它接受你需要为每个实现支持的类型,然后你可以使用注解处理器生成你的接口(interface)的扩展以具有编译时安全。

例如

@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.CLASS)
annotation class Input(
  val types: Array<KClass<*>> = []
)

@Input(types = [String::class, Double::class])
class DoSomethingB : IDoSomething<String> {

  override fun execute(vararg any: Any?): String = execute(any) { string, double ->
    println("Do something with parameters: $string, $double")
    ...
  }
}

// With an annotation processor you can generate an extension like this.
fun DoSomethingB.execute(vararg input: Any?, block: (string: String, double: Double) -> String): String {
  val string = input[0] as String
  val double = input[1] as Double
  return block(string, double)
}

关于generics - Kotlin 接口(interface)函数可互换参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60927539/

相关文章:

java - 在 ArrayList 中使用接口(interface)

Java泛型,将一个列表复制到另一个列表

数组中数组的 Swift 通用扩展

javascript - KotlinJS 中的 Hello World 未定义 Kotlin 失败

泛型:抽象类和子类型

java - 我的一些方法遇到了多个 "error: missing return statement"错误。我做错了什么以及如何解决?

java - 使用反射将子类类型传递给 Java 泛型方法不会引发异常

swift - 只能通过使用最终类来满足的 Swift 协议(protocol)要求

kotlin - 共享模块中的 KMM 依赖问题

map - 带有类型映射的数组