BigFoot
BigFoot

Reputation: 13

Kotlin Data Type Syntax

What does the following data type means:

() -> String

It is being used in the following context.

fun sampleFun(message: ()-> String)

Upvotes: 0

Views: 38

Answers (1)

Mohamed Rejeb
Mohamed Rejeb

Reputation: 2629

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

Upvotes: 1

Related Questions