在 kotlin 中使用高阶函数初始化 class
Initialize class with higher order functions in kotlin
Kotlin 中的高阶函数:
我有一个 class 接受 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> = {...}
}
现在,我无法理解如何初始化 class?
val obj = MyClass(
getData = {Data.getData ??? }
)
如何从 class 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)
}
使用成员函数参考:https://kotlinlang.org/docs/tutorials/kotlin-for-py/member-references-and-reflection.html
val obj = MyClass(getData = Data::getData)
Kotlin 中的高阶函数:
我有一个 class 接受 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> = {...}
}
现在,我无法理解如何初始化 class?
val obj = MyClass(
getData = {Data.getData ??? }
)
如何从 class 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)
}
使用成员函数参考:https://kotlinlang.org/docs/tutorials/kotlin-for-py/member-references-and-reflection.html
val obj = MyClass(getData = Data::getData)