- always in curly braces
{x: Int, y: Int -> x + y} - if a lambda is the last argument in a function call, it can be placed after the parentheses
list.maxByOrNull ({ it: Person -> it.age })
list.maxByOrNull () { it: Person -> it.age } - if the lambda is the only argument in a function call, the empty parentheses can be removed
list.maxByOrNull () { it: Person -> it.age }
list.maxByOrNull { it: Person -> it.age } - if the type of a lambda parameter can be inferred, you can omit the type
list.maxByOrNull { it: Person -> it.age }
list.maxByOrNull { it -> it.age } - if the context expects a lambda with only one argument, it is auto generated and the declaration can be omit
list.maxByOrNull { it -> it.age }
list.maxByOrNull { it.age } - if a lambda just delegates to a method or property, it can be replaced by a member reference
list.maxByOrNull { it.age }
list.maxByOrNull(Person::age)
Kotlin Lambda
最新推荐文章于 2024-04-15 09:18:41 发布