Kotlin Quick Start Guide by Marko Devcic

Kotlin Quick Start Guide by Marko Devcic

Author:Marko Devcic
Language: eng
Format: epub
Tags: COM051000 - COMPUTERS / Programming / General, COM051280 - COMPUTERS / Programming Languages / Java, COM051230 - COMPUTERS / Software Development and Engineering / General
Publisher: Packt Publishing
Published: 2018-08-31T07:05:02+00:00


Returning a value from a lambda

The previous click listener lambda had no return type. But, what if your lambda needs to return a value? Let's write a lambda that returns a sum of two integers that are passed as arguments:

{ a: Int, b: Int -> a + b }

You probably noticed that there is no return keyword. In fact, if we declared a return keyword, we'd get a compiler error stating that return is not allowed there:

// compiler error

{ a: Int, b: Int -> return a + b }

The return keyword is not allowed and Kotlin implicitly considers the last line of a lambda as a return expression. Of course, only if the lambda signature states that there should be a return value. In a void method, like the click listener example from the beginning, the last line is not considered as a return expression.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.