【问题标题】:Initialize class with higher order functions in kotlin在 kotlin 中使用高阶函数初始化类
【发布时间】:2020-12-16 01:46:36
【问题描述】:

Kotlin 中的高阶函数:

我有一个使用 wto 高阶函数的类:

data class Data(id: String)

class MyClass(
    private val getData: (find1: (String) -> Data?, find2: (String) -> Data?) -> (Request) -> Either<HttpError, Data>
) 

我在对象内部定义了getData函数。



object Data
{
  fun getData(find1: (String) -> Data?, find2: (String) -> Data?): (Request) -> Either<HttpError, Data> = {...}
}

现在,我无法理解如何初始化类?


 val obj = MyClass(
            getData = {Data.getData  ??? }
    )

如何从 MyClass 类中调用此函数:

repo.kt

override fun find1(value: String): Data? {
}

repo1.kt

override fun find1(value: String): Data? {
}

class MyClass(repo: Repo, repo1: Repo1, private val getData: (find1: (String) -> Data?, find2: (String) -> Data?) -> (Request) -> Either<HttpError, Data>)
{
    getData { id -> repo.find1(id) ?????}(request)
}

【问题讨论】:

    标签: kotlin higher-order-functions http4k


    【解决方案1】:

    使用成员函数参考:https://kotlinlang.org/docs/tutorials/kotlin-for-py/member-references-and-reflection.html

    val obj = MyClass(getData = Data::getData)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-08
      • 2015-09-11
      • 2021-05-10
      • 1970-01-01
      • 2019-08-21
      • 2021-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多