Kotlin functions and lambdas to send to Java developers

Function definition

The function is defined as follows.

//A function that increments and returns an Int type argument
fun increment(i: Int): Int {
    return i + 1
}

fun function name (argument list): return type This is the basic form.

Named arguments

Which value corresponds to which argument in a function call with many arguments It may be difficult to understand.

Kotlin has the ability to solve this problem.

/*
 *I will order takoyaki
 *
 * @param isKarashiMayo Whether to select mustard mayonnaise
 * @param isPutAonori Whether or not to put green laver
 *
 * @return An instance of takoyaki is returned
 */
fun oderTakoyaki(isKarashiMayo: Boolean, isPutAonori: Boolean): Takoyaki {
    // ...
}

val takoyaki = oderTakoyaki(true, false)
//Which flag is green laver?


val takoyaki = oderTakoyaki(isKarashiMayo = true, isPutAonori = false)
//You can give a formal argument name to the caller
//Easy to read code intent


val takoyaki = oderTakoyaki(isPutAonori = false, isKarashiMayo = true)
//If you give it a name, it doesn't have to be in the order defined in the function.

Default argument

I think there are many classes that have many overloads with different numbers of arguments. For example ...

By using default arguments in Kotlin, you can avoid such a situation where variations increase.

/*
 *I will order takoyaki
 *
 * @param isKarashiMayo Whether to select mustard mayonnaise
 * @param isPutAonori Whether or not to put green laver
 *
 * @return An instance of takoyaki is returned
 */
fun oderTakoyaki(isKarashiMayo: Boolean = false, isPutAonori: Boolean = true): Takoyaki {
    // ...
}

//Arguments can be omitted at the time of calling. If omitted, the default value is used.
val takoyaki = oderTakoyaki()

Extension function

You can add functions to existing classes later. Let's add a function to the String type that parses in our own date format.

fun String.toDate(): Date {
    val format = SimpleDateFormat("yyyy year MM month dd day")
    return format.parse(this)
}

val str = "March 09, 2019"
val date = str.toDate()

fun extended type .function name (argument list): return value Define it as ↑. By the way, the usual function is ... fun function name (argument list): return type ↑ It was like this. Preceding the function name with the extension target type makes it an extension function.

By the way, this extension target object is called ** receiver object ** in Kotlin.

Single expression function

If the function consists of one expression, the {} can be omitted. Single-expression functions can also omit the return type if the return type is inferrable.

//Normal function
fun double(x: Int): Int {
    return x * 2
}

//Described as a single expression function
fun double(x: Int): Int = x * 2

//Infer the return type with a single expression function
fun double(x: Int) = x * 2

Lambda expression

Lambda expressions are written as ↓.

val sum = { x: Int, y: Int ->
    x + y
}

{argument list-> body} Or if there are no arguments {Main body} It will be.

The implicit variable ʻit` can be used if the lambda expression has one parameter.

val double: (Int)->Int = {
    it * 2
}

The functional type is described as follows. (argument type)-> return type

inline function

Lambda expressions are a costly mechanism. Kotlin's lambda expression is implemented as an anonymous class. Each time a lambda expression that captures a variable is executed, an instance of the anonymous class is created.

So, the inline function. The inline function is compiled by replacing the function body where it is called.

inline fun edit(context: DataBaseContext, block: (DataBaseContext.Transaction)->Unit) {
    val transaction = context.beginTransaction() // 1.Transaction start
    block(transaction) // 2.Argument lambda execution
    transaction.commit() // 3.End of transaction
}

The inline function is effective A function that takes a lambda expression as an argument and executes it internally. The inline function also expands the argument lambda expression to the caller.

This is a sample that calls the Ueno inline function ʻedit`.

val dataBaseContext = DataBaseContext()

edit(dataBaseContext) { transaction ->
    val newObject = Any()
    transaction.add(newObject)
}

The ʻedit` function is expanded at compile time and looks like the following code (image)

val dataBaseContext = DataBaseContext()

//The edit function expands from here
val transaction = dataBaseContext.beginTransaction() // 1.Transaction start
val newObject = Any() // 2.Argument lambda expansion
transaction.add(newObject) // 2.Argument lambda expansion
transaction.commit() // 3.End of transaction
//The edit function expands so far

Another inline comes into play

Kotlin generics lose type information at run time.

For example, the following function will result in a compilation error.

fun <T> mapFilter(list: List<Any>): List<T> {
    return list.filter { it is T }
        .map { it as T }
}

This is because the type parameter T of the mapFilter function is lost at run time. Therefore, add the reified modifier to the inline function.

//↓ Here!
inline fun <reified T> mapFilter(list: List<Any>): List<T> {
    return list.filter { it is T }
        .map { it as T }
}

By doing this, it will be expanded while retaining the type information at runtime. The mapFilter function can now be compiled.

Recommended Posts

Kotlin functions and lambdas to send to Java developers
Kotlin scope functions to send to Java developers
Kotlin Class to send to Java developers
Kotlin Class part.2 to send to Java developers
Getting started with Kotlin to send to Java developers
I want to implement various functions with kotlin and java!
Kotlin's Null safety to send to Java developers
I want to transition screens with kotlin and java!
I tried to summarize the basics of kotlin and java
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
Differences between "beginner" Java and Kotlin
Generics of Kotlin for Java developers
[Android] Convert Android Java code to Kotlin
I want to return to the previous screen with kotlin and java!
Notes on building Kotlin development environment and migrating from Java to Kotlin
[Android] Convert Map to JSON using GSON in Kotlin and Java
Conversion between Kotlin nullable and Java Optional
Relationship between kotlin and java access modifiers
Convert all Android apps (Java) to Kotlin
Java implementation to create and solve mazes
[For beginners] Difference between Java and Kotlin
[Java] How to output and write files!
A Java engineer compared Swift, Kotlin, and Java.
Introduction to kotlin for iOS developers ⑥-Kotlin creation
Java: How to send values from Servlet to Servlet
Interoperability tips with Kotlin for Java developers
[Introduction to Java] Variable declarations and types
Introduction to kotlin for iOS developers ④-Type
Memo for migration from java to kotlin
Java to C and C to Java in Android Studio
Summary of good points and precautions when converting Java Android application to Kotlin
[Java] How to use FileReader class and BufferedReader class
Java switch statement and break, Kotlin when expression ...
Introduce Kotlin to your existing Java Maven Project
I want to send an email in Java.
[Java] How to get and output standard input
Migrate from Java to Server Side Kotlin + Spring-boot
Try to link Ruby and Java with Dapr
Java upload and download notes to Azure storage
Initial settings for rewriting Java projects to Kotlin
Introduction to kotlin for iOS developers ⑤-Practical XML
How to get and study java SE8 Gold
Introduction to kotlin for iOS developers ③-About gradle
Java8 to start now ~ forEach and lambda expression ~
What I did when I converted java to Kotlin
Introduction to kotlin for iOS developers ①-Environment construction
Where Java programmers tend to trip over Kotlin
Introduction to kotlin for iOS developers ②-Project creation
How to write Java String # getBytes in Kotlin?
What Java inexperienced people did to study Kotlin
How to access Java Private methods and fields
[Java] How to use Calendar class and Date class
Functions and methods
[Java] Introduction to Java
Java and JavaScript
XXE and Java
Introduction to java
How to call functions in bulk with Java reflection
Mastering Kotlin ~ Convert Java File to Kotlin File Road to Graduation ~ Part 3
I want to use Clojure's convenient functions in Kotlin