Recently I have seen a lot of interesting topics about Kotlin on portal.kotlin-academy.com/#/. Personally, I think it is very suitable for Kotlin lovers. Interested partners can consult it by themselves.

The interesting Kotlin series notes your understanding of each question.

0x08:What am I ?

fun main(args: Array<String>) {
    val whatAmI = {}()
    println(whatAmI)
}
Copy the code

What is the result of running the above code? Optional:

  1. “null”
  2. “kotlin.Unit”
  3. Doesn’t print anything
  4. Doesn’t compile

Think about it and write down the answers in your mind.

Analysis of the

Imperceptibly already to the eighth question, each look at a question, feel oneself still have harvest, and the knowledge point behind each question can be found in the official document basis, do the question at the same time also force oneself to read the official document repeatedly.

If you’ve read the previous seven questions carefully, you should be able to figure out the answer to this one very quickly:

Option 2: “Kotlin.unit”

If you have any questions, keep going. If you’re smart enough to get the right answer, feel free to move on.

As long as the type of variable whatAmI is clear, the problem will be solved.

Let’s separate the parentheses from the parentheses. What type of whatAmI is left with only the parentheses?

val whatAmI = {}
Copy the code

To the right of the equal sign is a Lambda expression of type ()-> Unit, which is the type of the variable wahtAmI. A Lambda expression followed by a () call to invoke() represents a call to the function type.

So what does a call to a function of type ()-> Unit return? Of course it’s Unit. Did I just talk nonsense? So, here we go

val whatAmI = {}()
Copy the code

WahtAmI is of type kotlin.unit, so the answer is kotlin.unit? Not rigorous enough. We also need to look at the JVM implementation logic of the println function, although it may be simple

/** Prints the given [message] and the line separator to the standard output stream. */
@kotlin.internal.InlineOnly
public actual inline fun println(message: Any?) {
    System.out.println(message)
}
Copy the code

To continue down

public void println(Object x) {
    String s = String.valueOf(x);
    synchronized (this) { print(s); newLine(); }}Copy the code

To the valueOf() method

public static String valueOf(Object obj) {
    return (obj == null)?"null" : obj.toString();
}
Copy the code

So, what the problem ends up printing to the console depends on the toString() method of kotlin.unit.

public object Unit {
    override fun toString(a) = "kotlin.Unit"
}
Copy the code

The correct answer to this question is:

Option 2: “Kotlin.unit”

extension

Let’s look at the implementation of the println() function as follows

/** Prints the given [message] and the line separator to the standard output stream. */
@kotlin.internal.InlineOnly
public actual inline fun println(message: Any?). {
    System.out.println(message)
}
Copy the code

An actual keyword appears, some readers may not be clear, a little explanation.

Kotlin has been designed as a cross-platform language since its inception, with the aim of allowing developers to use Kotlin to develop applications for any platform. But in some cases developers may need to write different code for different platforms. In this case, we need to define classes or methods that need to be implemented on different platforms in the common code by using the Expect keyword, and then implement the corresponding classes or methods in the corresponding directories of each platform by using the actual keyword, which is Kotlin’s Expect /actual mechanism. So let’s find the expect function for println().

In a similar way, we can find println() implementations for other platforms

Refer to the official explanation question: kotlinlang.org/docs/mobile…