I'm reading through some code that I didn't write and I've come across this instance variable within a class.
private val t: (Duration) -> String?
It's called later in the code by
val str = t(<Insert Duration here>)
What does this syntax mean in Kotlin? It's difficult to interpret the variable names, unfortunately, so I have no clue what this is doing.
CodePudding user response:
(Duration) -> String?
is the type of the variable t
. In particular here, it's a function type.
It means "function that takes a Duration
as argument and returns a nullable String
".
The other piece of code shows that the variable t
is indeed being used as a function afterwards:
val str = t(Duration.seconds(3))
// which could also be written as
val str = t.invoke(Duration.seconds(3))