Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use kotlin top-level functions and function expressions

Shulou Source: shulou.com Published: 2022-06-01 06:52:24 09月25日 Update

This article will explain in detail how to use kotlin top-level functions and function expressions. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Top-level function

Unlike functions that can only be defined in each class in Java, Kotlin adopts the practice in JavaScript and can define functions anywhere in the file, which is called top-level function.

After compilation, the top-level function becomes a static function under the file class. For example, the joinToString function defined under the file name join.kt can be called through JoinKt.joinToSting, where JoinKt is the compiled class name.

/ / compiled into a static function / / filename join.ktpackage stringsfun joinToString (): String {...} / * Java * / import strings.JoinKt;JoinKt.joinToSting (....)

Take a look at the compiled effect of the above function: / / decompiled result after being compiled into a class file

/ / decompilation result @ NotNullpublic static final String joinToString (@ NotNull Collection collection, @ NotNull String separator, @ NotNull String prefix, @ NotNull String postfix) {Intrinsics.checkParameterIsNotNull (collection, "collection"); Intrinsics.checkParameterIsNotNull (separator, "separator"); Intrinsics.checkParameterIsNotNull (prefix, "prefix"); Intrinsics.checkParameterIsNotNull (postfix, "postfix"); StringBuilder sb = new StringBuilder (prefix); int index = 0; for (Iterator var7 = (Iterable) collection). Iterator () Var7.hasNext (); + + index) {Object element = var7.next (); if (index > 0) {sb.append (separator);} sb.append (element);} sb.append (postfix); String var10000 = sb.toString (); Intrinsics.checkExpressionValueIsNotNull (var10000, "sb.toString ()"); return var10000 } / / default function values public static String joinToString$default (Collection var0, String var1, String var2, String var3, int var4, Object var5) {if ((var4 & 2)! = 0) {var1 = "";} if ((var4 & 4)! = 0) {var2 = "[";} if ((var4 & 8)! = 0) {var3 = "]";} return joinToString (var0, var1, var2, var3)

Next, let's take a look at a very important feature in Kotlin, the extension function.

Extended function

An extension function is a member function of a class, but it is defined outside the class

Extension functions cannot access private or protected members

Extension functions are also compiled into static functions

Expression function body

Take a look at the concepts related to the function declaration through the following simple example. The keyword of the function declaration is fun, which is, well, simpler than JS's function.

In Kotlin, the parameter type is placed in the variable: after that, so is the function return type.

Fun max (a: Int, b: Int): Int {if (a > b) {return a} else {return b}}

Of course, Kotlin has the function of type derivation, and if you can derive the type from the function expression, you can also not write the return type.

But the above is still a bit tedious and simple. In Kotlin, if is an expression, that is, it has a return value, so you can return directly. In addition, only one line and one sentence in the judgment formula can omit the braces:

Fun max (a: Int, b: Int) {return if (a > b) an else b}

Can it be any easier? Yes, if is an expression, so it can be returned through the body of the expression function:

Fun max (a: Int, b: Int) = if (a > b) an else b

In the end, all it takes is one line of code.

This is the end of this article on "how to use kotlin top-level functions and function expressions". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Function compilation expression file top level type article static one line can be passed member file name more result good practical important tedious that is code Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Shulou Information NVidia macOS Linux