Android Development with Kotlin by Marcin Moskala & Igor Wojda

Android Development with Kotlin by Marcin Moskala & Igor Wojda

Author:Marcin Moskala & Igor Wojda [Moskala, Marcin]
Language: eng
Format: azw3, epub
Publisher: Packt Publishing
Published: 2017-08-30T04:00:00+00:00


// Tester1.kt

fun main(args: Array<String>) { a() }

// Tester2.kt

inline fun a() { b() }

private fun b() { print("B") }

How is it possible? For the internal modifier it is simpler, because the internal modifier is public under the hood. For private functions, there is an additional access$b function created that has public visibility and that is only invoking the b function:

public static final void access$b() { b(); }

This behavior is presented here just to explain why less restrictive modifiers can sometimes be used inside inline functions (these situations can be found in Kotlin standard library in Kotlin 1.1). In the projects, we should design elements in such a way that there is no need to use such suppressions.

Another problem is less intuitive. While no lambda has been created, we cannot pass parameters that are of the function type to another function. Here is an example:

fun boo(f: ()->Int) { //... } inline fun foo(f: () -> Int) { boo (f) // ERROR, 1 }

When function is inline, then its function arguments cannot be passed to function that are not inline.

This doesn't work because no f parameter has been created. It has just been defined to be replaced by the function literal body. This is why it cannot be passed to another function as an argument.

The simplest way to deal with it is by making the boo function inline as well. Then it will be OK. In most cases, we cannot make too many functions inline. Here are a few reasons why:

The inline functions should be used for smaller functions. If we are making inline functions that are using other inline functions, then it can lead to a large structure being generated after compilation. This is a problem both because of compilation time and because of the resulting code's size.

While inline functions cannot use element with visibility modifiers more strict than the one they have, it would be a problem if we would like to use them in libraries where as many functions as possible should be private to protect the API.



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.