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


边栏推荐
- 大地坐标系转换火星坐标系
- Nowcode- learn to delete duplicate elements in the linked list (detailed explanation)
- Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
- 一小时内学会Abaqus脚本编程秘籍
- HM secondary development - data names and its use
- redis源码优化--绑核
- Sort 2 bubble sort and quick sort (recursive and non recursive explanation)
- About standard IO buffers
- Rosen's QT journey 102 listmodel
- 解决uniapp等富文本图片宽度溢出
猜你喜欢

有趣的 Kotlin 0x07:Composition

IM即时通讯开发优化提升连接成功率、速度等

MySQL5.7及SQLyogV12安装及使用破解及常用命令

Leetcode learn to insert and sort unordered linked lists (detailed explanation)

Introduction and implementation of queue (detailed explanation)

Learn ABAQUS script programming script in an hour

HM二次开发 - Data Names及其使用

Sort 1-insert sort and Hill sort

USB产品(FX3、CCG3PA)的调试方法

The epidemic dividend disappeared, and the "home fitness" foam dissipated
随机推荐
使用js直传oss阿里云存储文件,解决大文件上传服务器限制
Abaqus GUI界面解决中文乱码问题(插件中文乱码也适用)
HyperMesh auto save (enhanced) plug-in instructions
Qt学习之安装
PHP gets the applet code, and the applet jumps with parameters
Multiple commands produce '... /xxx.app/assets.car' problem
重置grafana登录密码为默认密码
Headline article_ signature
nowcode-学会删除链表中重复元素两题(详解)
LeetCode-学会对无序链表进行插入排序(详解)
QT packaging
Geodetic coordinate system to Martian coordinate system
Early in the morning, pay Bora SMS to say that you won the "prize"? Dealing with server mining virus - kthreaddi
"Wei Lai Cup" 2022 Niuke summer multi school training camp 3 a.ancestor lca+ violence count
WSL+Valgrind+Clion
Microsoft question 100 - do it every day - question 11
I can only sell the company after the capital has been "cut off" for two years
有趣的 Kotlin 0x06:List minus list
Nowcode- learn to delete duplicate elements in the linked list (detailed explanation)
Leetcode daily practice - the number of digits in the offer 56 array of the sword finger