kotlinhigher-order-functionshttp4k

Initialize class with higher order functions in kotlin


Higher Order Functions in Kotlin:

I have a class that takes wto higher order functions:

data class Data(id: String)

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

I am defined getData function inside the object.



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

Now, I am not able to understand how to initialize the class?


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

How do I call this function from 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)
}

Solution

  • Use member function reference: https://kotlinlang.org/docs/tutorials/kotlin-for-py/member-references-and-reflection.html

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