c99374747d884571d488d224b1fa0f6b by 2024

c99374747d884571d488d224b1fa0f6b by 2024

Author:2024
Language: eng
Format: epub
Published: 2024-06-29T17:08:29+00:00


Chapter 11

Anonymous, Lambdas, Object & Inline Functions

Languages that don't allow functions to exist as entities in their own right make things difficult without many advantages. Kotlin introduces features to allow functions to be treated in more flexible ways, in particular to be used as parameters to other functions. In this chapter we look at anonymous functions, object functions, function references, lambda functions and inline functions.

Many of Kotlin's language innovations are about how you can use functions.

As well as being able to use standalone "classical" functions, Kotlin also provides function expression and references, which allow you to pass functions to other functions. One of the problems is that this has resulted in a confusing menagerie of new and different function types.

When it comes to functions in Kotlin, there is usually more than one way to get a particular job done.

Anonymous Functions & References

What isn't as well known as it should be, is that you can create a variable of a function type and you can store a reference to a function in it, for example: typealias arith = (Int,Int)->Int

var myfunc:arith

myfunc = fun(a:Int,b:Int):Int{return a+b}

println(myfunc(1,2))



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.