Home > Back-end >  Kotlin Data Type Syntax
Kotlin Data Type Syntax

Time:06-22

What does the following data type means:

() -> String

It is being used in the following context.

fun sampleFun(message: ()-> String)

CodePudding user response:

this is a lambda function, this code means that this is a lambda function that return a string:

() -> String

and here it's passed as a parameter named message:

fun sampleFun(message: ()-> String)

so when you call sample fun you need to add message parameter like that:

sampleFun(
    message = {
        "Hello" // this means that when you call message it will return this String
    }
)

take a look on this link if want to learn more about lambda functions in kotlin and how to use them correctly

  • Related