Questions tagged [kotlin-function-type]

10 questions
4
votes
1 answer

kotlin passing function as argument got Type mismatch, Required: ()->Unit, Found: Unit

Having a function which takes function type for the callback fun dataBtnClickHandler(callbackFunc: () -> Unit){ // do something ...... // call the callback function callbackFunc } And there is a function defined as private…
lannyf
  • 6,775
  • 4
  • 49
  • 92
3
votes
3 answers

Why are there two functions which do the exact same thing? Why must be invoked differently in Kotlin?

fun main() { fun evenFn(num: Int): Boolean { return num % 2 == 0 } val evenFn = fun(num: Int) = num % 2 == 0 val list = listOf(1, 2, 3, 4, 5, 6) println(list.filter(evenFn)) println(list.filter { evenFn(it) }) } How…
Shubhang B
  • 59
  • 1
  • 3
2
votes
0 answers

How To Obtain An Annotation Of A Function Literal At Runtime

package _z_additional import org.junit.jupiter.api.Test import org.junit.jupiter.api.TestInstance import kotlin.reflect.KFunction import kotlin.reflect.full.declaredMemberFunctions import…
Sero
  • 1,572
  • 1
  • 15
  • 29
2
votes
3 answers

Why String.() -> Unit is the same as (String) -> Unit in my code?

I am struggling to understand how a function type with receiver works in Kotlin. I don't understand why we can use (String) -> Unit and String.() -> Unit in my code indistinctively fun main() { var showStringFunction1: (String) -> Unit =…
1
vote
2 answers

How do I provide an implementation for a function type?

Concerning function types in Kotlin The following gives exception kotlin.UninitializedPropertyAccessException: lateinit property foo has not been initialized class SomeClass (){ lateinit var foo: (String) -> Int } val result : Int =…
Jim
  • 2,341
  • 1
  • 11
  • 23
1
vote
2 answers

Call Higher Order function of Kotlin from Java

Kotin Class class LoginService{ fun getLoginData(loginData: String) { request(LoginApi.create().getLoginData(loginData))} } fun changePassword(_:String){ request(LoginApi.create().changePassword(_) } class…
0
votes
1 answer

Inherit function type in Kotlin

Is there a way in Kotlin to inherit function types? What I want is to have a member like so: private lateinit var onPickedFunc: (Any) -> Task? so I can dynamically store the functions I need to run after a user has made it's choice. For…
Omer Levy
  • 75
  • 1
  • 7
0
votes
2 answers

Why should I implement a function type as an interface in Kotlin

I came across something and wondered all the time why you should do this. You implement an interface in Kotlin through a simple function type: "It is possible for a class to implement a function type as if it were an interface. It must then supply…
Torben G
  • 641
  • 7
  • 21
0
votes
1 answer

Kotlin and how to convert an if/else to functional approach?

I have a function in this format: fun myFunction(param1: Int, param2: String, param3: Number): Number = if(param1 < NUMBER) { if(param2 == "FOO") { // code for result } else { // code for result …
Jim
  • 2,341
  • 1
  • 11
  • 23
0
votes
2 answers

Extension function on a generic type

I am new in Kotlin and I am trying to understand type alias and functions. I have the following example: interface EmptyInterface typealias GenericCase = T.(EmptyInterface) -> T val myFunctionVariable: GenericCase = { _ -> "Hello…
Jim
  • 2,341
  • 1
  • 11
  • 23