Kotlin Tutorial Learn-run, tag label, function Function-type

Source: Internet
Author: User

In Java, you can use {} to create an anonymous block of code, and the code block will be executed normally, and it doesn't seem to have any effect other than changing the scope. But it is not possible to do so in Kotlin.

In fact, we all know that a function must be associated with a memory address, and an anonymous block of code is actually an anonymous function. In Kotlin, you typically use the run function to run an anonymous block of code.

As follows:

Using an identifier followed by the @ symbol in Kotlin to define a label, using the @ followed by the identifier to refer to a label, the run function syntax appears to be somewhat strange at first, in fact, the run function with a function as a parameter, and an anonymous block of code can be an anonymous function, when we are in the IntelliJ When you place the mouse cursor over the curly braces of an anonymous code block in idea, the function signature corresponding to the anonymous code block is displayed

Local final Fun <anonymous> (): Int. defined in Com.kotlin_learn.control_flow.fun_run

Local represents the scope, defined inside the function, the scope is only local,final for immutable,<anonymous> that is anonymous, () is the argument list, int is the return value, Com.kotlin_learn.control_ Flow.fun_run is the full path to the location where the function is defined.

Thus, the parameters of the run function, that anonymous code block is converted by the compiler to an anonymous function is no doubt.

Of course, the run function can have a return value, so an anonymous code block can also be an anonymous function with a return value.

Each function corresponds to one or more addresses, and every label label corresponds to one or more addresses, so the function itself is a label.

So you can use the [email protected] syntax.

anonymous function Although there is no function name, but we can define a named tag to represent this function, so you can use [email protected] 2 to return 2 to I.

One might wonder why the return 2 is not used directly because return will be returned from the Fun_run function, not from the anonymous function.

Questions about return and the definition of function nesting are also stated below.

The result of this code operation is as follows:


The next thing to say about the Foreach function, unlike other languages, is that foreach is not a syntax in Kotlin, but a class of functions, and foreach is a function of iterator, and any class that implements iterator can use foreach. The argument to the Foreach function is also a function whose argument is a template function, which can be a named function, an anonymous function, and a lambda. foreach invokes the incoming function once for each element of the iterator iteration.

foreach is used to familiarize yourself with the use of tags and return.

As follows:

Don't forget the infix function call.

The results of the operation are as follows:

Next, talk about function type, and discuss the functions.

For programmers, functions are familiar, but are we really familiar with functions?

A function can also be a type, which can be a variable, or even a constant.

As follows:

In this code, we define variables and constants for several function types, and perform operations such as assigning values to a function pointer in C + +, but there is a unique place. For example, a nested function definition, a reference to a function tag, a tagged return value, and so on. We also see how a lambda expression is used to define the parameter list of a function and the return type of an automatic judgment in an anonymous function (the one represented by End1).

The result of this code operation is as follows:

Just now we mentioned the definition of function nesting, which is a place to be careful.

Look at the following code:

A programmer from java/c++ (like me:) can easily consider the nested function definition here as a nesting of anonymous blocks of code, assuming that the program executes from outside. There is no such problem with programmers from PASCAL/PL, and remember that functions can be nested but not executed from the outside if they are not called.

So the output is simple:


We also see the use of return, where F1,F2,F3 is an internally defined function, but can still be used as a label.

Visible Kotlin is a synthesis of the language, and even borrowed from the old Unix script and Pascal syntax, draw on the characteristics of many languages, coupled with a powerful compiler (Kotlin compiler will help you do a lot of things, far more than other languages do), The Kotlin code is very concise and elegant, and the programming is quite flexible and efficient.

In Kotlin, too many functions are implemented by using functions as parameters, some cannot be called syntax, but functions are nested, infix function call,lambda, function parameters, variable argument lists, flexible tags, powerful return, automatic type judgments, Range,iterator, operator overloading, omitting, template ... These are too many features that cause Kotlin's syntax to dazzle, though sometimes it may seem elegant, but it can also cause great difficulty in reading the code.

End this article with a piece of code with no eggs.

Kotlin Tutorial Learn-run, tag label, function Function-type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.