is there any way I send a nullable Function<T,R> as parameter in Kotlin?

jpganz18

I am trying to use the public interface Function (as I learned it in Java) in Kotlin.

For this I created my method

fun foo(input: List<String>, modifier1: Function<List<String>>? = null){

}

as far I remember here I should be able to do modifier1.apply(input)

but seems like it is not possible (it is possible to do modifier1.apply{input} though)

Reading more about it I found this:

Kotlin: how to pass a function as parameter to another?

So I changed my method signature to this:

fun foo(input:String,  modifier2: (List<String>) -> (List<String>){
}

Here I am able to do modifier2(input)

and I can call foo this way

service.foo(input, ::myModifierFunction)

where

fun myModifierFunction(input:List<String>):List<String>{
   //do something
   return input
}

So far this seems possible but it is not acceptable to have the function reference as nullable, is there any way I can do that? or use Function ?

Roland

You were using kotlin.Function instead of java.util.function.Function in your first example. Note that the latter takes 2 generic types: 1 for the incoming parameter and 1 for the resulting one.

The apply method you saw is the default Kotlin one: apply, not the one of Java's Function-interface.

If you really want to have the Java-function as nullable type the following should work:

fun foo(input: List<String>, modifier1: java.util.function.Function<List<String>, List<String>>? = null) {
    modifier1?.apply(input) ?: TODO("what should be done if there wasn't passed any function?")
}

Kotlin variant for the same:

fun foo(input: List<String>, modifier1: ((List<String>) -> List<String>)? = null) {
     modifier1?.invoke(input) ?: TODO("what should be done if there wasn't passed any function?")
}

Maybe also a default function, such as { it } instead of null might better suite your needs? (Java variant would be Function.identity()):

// java   modifier1 : Function<List<String>, List<String>> = Function.identity()
// kotlin modifier1 : (List<String>) -> List<String> = { it }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Is there any way that I can get a parameter in paintComponent?

Is there any easy way to see what exceptions a Kotlin function throws?

Can I send extension function through function parameter

Why don't I get a warning while passing parameter to a function which should not accept any incoming parameter?

Passing a `Nullable<T>` as a Type parameter to a C# function

Declare a function generic type parameter in a way that allows both for nullable types and using that type parameter to declare `KClass<T>`

How to declare a nullable function Parameter in kotlin

Can I send parameter to the Interface in kotlin?

Create Arraylist as parameter of a function that accepts any type in Kotlin

Is there any way to pass an array of an unknown type as a parameter to a function in C?

Is there any way to send data as object instead of Parameter in Alamofire?

Is there an elegant kotlin way of convincing the compiler that a nullable field to which I just assigned a real value can't be null anymore?

Why I can't assign nullable type to Any in Kotlin?

Nullable <T> as parameter

Is there a way I can make a function a parameter?

How do I specify "any non-nullable type" as a generic type parameter constraint?

Is there any way in Kotlin to add a custom function to a property inside an interface?

Is there any way to "hide" a Kotlin function from Retrofit annotation processor?

Is there any way to declare T in constructor in Kotlin?

Is there any way that i can perform sum instead of count using cut or any other function using R

when I do the "saveUser" function with bcrypt in password postman send me "couldn't get any response"

Is there any way to get list of arguments in Kotlin for function

Problem in calling function as a send parameter in Kotlin retrofit

How can I pass in any function as a parameter?

Kotlin get function. Should be simple but I don't know how. Any assistance would be appreciated

Is there any way to constrain a Kotlin type parameter to an interface type?

Kotlin call function with parameter by using Any is xxx || Any is yyy not working

A C# function that returns a nullable type for any T - reference or value

Any possible way to use fixed and multiple function argument types in Kotlin?