当前位置:网站首页>Interesting kotlin 0x08:what am I
Interesting kotlin 0x08:what am I
2022-07-28 16:45:00 【AndroidKt】
Recently http://portal.kotlin-academy.com/#/ I see a lot about Kotlin An interesting topic . I think it's very suitable for Kotlin lovers , If you are interested, you can check it by yourself .
【 amusing Kotlin 】 Record your understanding of each question in the series .
0x08:What am I ?
fun main(args: Array<String>) {
val whatAmI = {
}()
println(whatAmI)
}
Above code , What is the result of the operation ? optional :
- "null"
- "kotlin.Unit"
- Doesn’t print anything
- Doesn’t compile
Think about it , Record the answers in your mind .
analysis
Unknowingly, it has reached question 8 , Every time you look at a question , I feel I have something to gain , And the knowledge behind each question can be found in the official documents , While doing the questions, I also force myself to read the official documents again and again .
If you have carefully read the first seven questions, you should be able to get the answer to this question soon :
Options 2 :"kotlin.Unit"
Friends in doubt go on , If you're smart and you've got the right answer , Please move freely .
As long as the variables are clear whatAmI The type of , The problem is solved .
Let's look at parentheses and parentheses separately , Leaving only large brackets whatAmI What type is it ?
val whatAmI = {
}
To the right of the equal sign is a Lambda expression , The type is ()-> Unit , Namely variable wahtAmI The type of , and Lambda The expression is followed by () In fact, it's called invoke() function , Represents a call to a function type .
that , One type is ()-> Unit What is returned after the function type is called ? Naturally Unit , Did I say a word of nonsense . therefore , In question
val whatAmI = {
}()
wahtAmI The type of kotlin.Unit, So the answer is kotlin.Unit ? Not rigorous enough . We need to see again println Function in JVM Implementation logic on the platform , Although the content 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)
}
Keep going
public void println(Object x) {
String s = String.valueOf(x);
synchronized (this) {
print(s);
newLine();
}
}
Until then valueOf() Method
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
therefore , The final output of the title to the console depends on kotlin.Unit Of toString() Method .
public object Unit {
override fun toString() = "kotlin.Unit"
}
here , We can say that the correct answer to this question is :
Options 2 :"kotlin.Unit"
extend
We see the println() The function is implemented with the following code
/** 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)
}
Appear one actual keyword , Maybe some readers don't know , A little explanation .
Kotlin Language starts from the beginning of design , Positioning is cross platform language , Designed for developers to use Kotlin Language to develop applications on any platform . But in some cases, developers may need to write different code for different platforms . Now we need to go through expect Keywords define classes or methods that need to be implemented on different platforms in the common code part , Then, in the directory corresponding to each platform, through actual Keyword to implement the corresponding class or method , This is Kotlin Of expect/actual Mechanism . Let's look for println() Corresponding expect function .


In a similar way , We can also find other platforms println() Realization

Quote official pictures to illustrate the problem :https://kotlinlang.org/docs/mobile/connect-to-platform-specific-apis.html


边栏推荐
- 使用js直传oss阿里云存储文件,解决大文件上传服务器限制
- 快速掌握 Kotlin 集合函数
- Leetcode daily practice - the number of digits in the offer 56 array of the sword finger
- [pointer internal skill cultivation] character pointer + pointer array + array pointer + pointer parameter (I)
- PHP calculate coordinate distance
- Learn ABAQUS script programming script in an hour
- Li Hongyi, machine learning 5. Tips for neural network design
- 日常开发方案设计指北
- Each account corresponds to all passwords, and then each password corresponds to all accounts. How to write the brute force cracking code
- Sort 2 bubble sort and quick sort (recursive and non recursive explanation)
猜你喜欢

ANSA二次开发 - Visual Studio Code上搭建ANSA二次开发环境

Headline article_ signature

LeetCode-学会复杂带随机指针链表的题(详解)

Ansa secondary development - Introduction to interface development tools

500million users, four years earlier than wechat... This app, which has been in operation for 15 years, will be permanently discontinued

About the web docking pin printer, lodop uses

一大早支付宝来短信说你中“奖”了?处理服务器挖矿病毒 - kthreaddi

The epidemic dividend disappeared, and the "home fitness" foam dissipated

HyperMesh自动保存(增强版)插件使用说明

WSL+Valgrind+Clion
随机推荐
Leetcode daily practice - the number of digits in the offer 56 array of the sword finger
Sort 5-count sort
“蔚来杯“2022牛客暑期多校训练营3 ACFHJ
有趣的 Kotlin 0x09:Extensions are resolved statically
Asp.net large file block upload breakpoint resume demo
HyperMesh auto save (enhanced) plug-in instructions
QT designer for QT learning
"Wei Lai Cup" 2022 Niuke summer multi school training camp 3 acfhj
小程序:scroll-view默认滑倒最下面
Rosen's QT journey 101 models and views in QT quick
Sort 1-insert sort and Hill sort
First day of QT study
Design direction of daily development plan
Im im development optimization improves connection success rate, speed, etc
Some suggestions on optimizing HyperMesh script performance
Qt学习之信号和槽机制
【指针内功修炼】字符指针 + 指针数组 + 数组指针 + 指针参数(一)
有趣的 Kotlin 0x06:List minus list
Applet: get element node information
redis源码优化--绑核