当前位置:网站首页>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


边栏推荐
- What does it remote operation and maintenance mean? Which is the best remote operation and maintenance software?
- About standard IO buffers
- LwIP development | socket | UDP
- Each account corresponds to all passwords, and then each password corresponds to all accounts. How to write the brute force cracking code
- IM即时通讯软件开发网络请求成功率的优化
- “蔚来杯“2022牛客暑期多校训练营3 H.Hacker SAM+线段树/DP/分治(不带修查区间最大子段和)
- ANSA二次开发 - Apps和ANSA插件管理
- Redis series 4: sentinel (sentinel mode) with high availability
- Solve the width overflow of rich text pictures such as uniapp
- Redis系列4:高可用之Sentinel(哨兵模式)
猜你喜欢
![[pointer internal skill cultivation] character pointer + pointer array + array pointer + pointer parameter (I)](/img/e8/2044cae63fe2145ce6294cb1fdfaa0.png)
[pointer internal skill cultivation] character pointer + pointer array + array pointer + pointer parameter (I)

Configure HyperMesh secondary development environment on vs Code

IM即时通讯软件开发网络请求成功率的优化

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

Im im development optimization improves connection success rate, speed, etc

MySQL view event status statements and modification methods

Headline article_ signature

Sort 5-count sort

Leetcode daily practice - the number of digits in the offer 56 array of the sword finger

I can only sell the company after the capital has been "cut off" for two years
随机推荐
HyperMesh运行脚本文件的几种方法
“蔚来杯“2022牛客暑期多校训练营3 J.Journey 0-1最短路
Design direction of daily development plan
Li Hongyi, machine learning 5. Tips for neural network design
"Weilai Cup" 2022 Niuke summer multi school training camp 3 h.hacker sam+ segment tree /dp/ divide and conquer (without the largest sub segment and of the inspection interval)
小程序:scroll-view默认滑倒最下面
Splash (渲染JS服务)介绍安装
Leetcode learn to insert and sort unordered linked lists (detailed explanation)
Im im development optimization improves connection success rate, speed, etc
The epidemic dividend disappeared, and the "home fitness" foam dissipated
Hdu1847 problem solving ideas
PHP 图片上传
Thoughts on solving the pop-up of malicious computer advertisements
QT packaging
ticdc同步数据怎么设置只同步指定的库?
Multiple commands produce ‘.../xxx.app/Assets.car‘问题
Configure HyperMesh secondary development environment on vs Code
2021-04-02
小程序:获取元素节点信息
Wei Jianjun couldn't catch up with Li Shufu by riding a BMW