0

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 类中调用此函数:

报告.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)
}
4

1 回答 1

1

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

val obj = MyClass(getData = Data::getData)
于 2020-08-27T08:21:10.470 回答