Home > Mobile >  Kotlin list in the operation of a getOrElse function, look not to understand the lambda is what mean
Kotlin list in the operation of a getOrElse function, look not to understand the lambda is what mean

Time:05-21


Function are as the following:
GetOrElse () function is used to calculate the default value, if there is no index in the collection, it returns the default value,

The function USES the example:
 val Numbers=listOf (' 1 ', '2', '3', '4') 
Println (Numbers. GetOrElse (6, {} 'c'))

The realization of the getOrElse () :
 the inline fun & lt; T> List GetOrElse (
Index: Int,
DefaultValue: (Int) - & gt; T//this (Int) - & gt; T look not to understand
) : T

My question is: the lambda expressions (Int) - & gt; What is the meaning of T ?
My understanding is one way to receive at the beginning, input Int type parameters, output type T,
However, in the above example, {} 'c' is a character not Int type?
thank you

CodePudding user response:

The output is "T" type, which is a generic, you this is not a set of generic character?
ListOf (' 1 ', '2', '3', '4'), this is not a character is what?

CodePudding user response:

The 'c' is the return value, return can omit kotlin lambda expressions, int as input parameters, under the getOrElse method should also didn't write the whole body, the simplest is
 the inline fun & lt; T> List GetOrElse (
Index: Int,
DefaultValue: (Int) - & gt; T//this (Int) - & gt; T look not to understand
) : T {
Return defaultValue (index)
}


DefaultValue (index), this index is introduced into the int
  • Related