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


边栏推荐
- Ansa secondary development - build ansa/meta secondary development environment on pycharm
- Redis series 4: sentinel (sentinel mode) with high availability
- HyperMesh auto save (enhanced) plug-in instructions
- 在vs code上配置Hypermesh二次开发环境
- 每一个账号对应所有密码,再每一个密码对应所有账号暴力破解代码怎么写?...
- Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
- The little red book of accelerating investment, "rush to medical treatment"?
- Qt学习之安装
- ticdc同步数据怎么设置只同步指定的库?
- 日常开发方案设计指北
猜你喜欢

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

QT packaging

ANSA二次开发 - 在PyCharm上搭建ANSA/META二次开发环境

有趣的 Kotlin 0x09:Extensions are resolved statically

Some suggestions on optimizing HyperMesh script performance

About the web docking pin printer, lodop uses

Leetcode daily practice - 160. Cross linked list

Optimization of network request success rate in IM instant messaging software development

Two of C language programming!! Role of

Headline article_ signature
随机推荐
WSL+Valgrind+Clion
有趣的 Kotlin 0x08:What am I
Detailed record of steps to configure web server (many references)
I can only sell the company after the capital has been "cut off" for two years
每一个账号对应所有密码,再每一个密码对应所有账号暴力破解代码怎么写?...
The local area network cannot access the Apache server
500million users, four years earlier than wechat... This app, which has been in operation for 15 years, will be permanently discontinued
小程序:获取元素节点信息
排序4-堆排序与海量TopK问题
Leetcode daily practice - the number of digits in the offer 56 array of the sword finger
nowcode-学会删除链表中重复元素两题(详解)
Redis系列4:高可用之Sentinel(哨兵模式)
About the web docking pin printer, lodop uses
WSL+Valgrind+Clion
PHP 图片上传
Configure HyperMesh secondary development environment on vs Code
配置web服务器步骤详细记录(多有借鉴)
2021-04-02
有趣的 Kotlin 0x0A:Fun with composition
关于MIT6.828_HW9_barriers xv6 homework9的一些问题