当前位置:网站首页>有趣的 Kotlin 0x07:Composition
有趣的 Kotlin 0x07:Composition
2022-07-28 15:42:00 【AndroidKt】
最近在 http://portal.kotlin-academy.com/#/ 上看到很多关于 Kotlin 的有趣的题目。个人觉得很适合 Kotlin 爱好者,感兴趣的小伙伴可自行查阅。
【有趣的 Kotlin 】系列记录自己对每一题的理解。
0x07:Composition
operator fun (() -> Unit).plus(f: () -> Unit) = {
this()
f()
}
fun main(args: Array<String>) {
({
print("Hello, ") } + {
print("World") })()
}
以上代码,运行结果是什么?可选项:
- "Hello, World"
- Error: Expecting top-level declaration
- Error: Expression f cannot be invoked as a function
- Error: Unresolved reference (operator + not defined for this types)
- Works, but prints nothing
思考一下,记录下你心中的答案。
分析
上一篇文章我们提到过,Kotlin 中使用 operator 关键字用于修饰函数,表示该函数重载一个操作符或者实现一个约定。使用 operator 关键字修饰函数并且函数名只能为component1、component2、component3 … 时则是实现一个约定,即 解构 。
题目中这个操作符重载函数,我们分析下

重载函数的 receiver ,参数以及返回值均为函数类型
() -> Unit
再看看 main() 函数,对照上方操作符重载函数一起查阅。

所以,正确答案为
选项 1 :"Hello, World"
延伸
val aa = println("hello")
val a = {
println("hello") }
fun b() = println("b")
fun c() = {
println("c") }
fun d(e: () -> Unit) {
e()
}
fun f(e: () -> Unit) = {
e()
}
fun main(){
println(aa)
println(a)
b()
c()()
c().invoke()
d(c())
f(c())()
}
随机手写一题,望各位不吝赐教。
总结
Function:https://kotlinlang.org/docs/functions.html
Lambdas:https://kotlinlang.org/docs/lambdas.html
Inline functions:https://kotlinlang.org/docs/inline-functions.html
边栏推荐
- 在vs code上配置Hypermesh二次开发环境
- 关于MIT6.828_HW9_barriers xv6 homework9的一些问题
- c语言编程当中两个!!的作用
- nowcode-学会删除链表中重复元素两题(详解)
- Thoughts on solving the pop-up of malicious computer advertisements
- Fx3 development board and schematic diagram
- Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
- 1. Simple command line connection to database
- Hdu1847 problem solving ideas
- Redis series 4: sentinel (sentinel mode) with high availability
猜你喜欢

Sort 4-heap sort and massive TOPK problem

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

Headline article_ signature

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

Sort 3-select sort and merge sort (recursive implementation + non recursive implementation)

What does it remote operation and maintenance mean? Which is the best remote operation and maintenance software?
![[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)

About standard IO buffers

About the web docking pin printer, lodop uses

ANSA二次开发 - Apps和ANSA插件管理
随机推荐
Qt学习第一天
Wei Jianjun couldn't catch up with Li Shufu by riding a BMW
配置web服务器步骤详细记录(多有借鉴)
Use js direct OSS to store files in Alibaba cloud and solve the limitation of large file upload server
Using pyqt to design gui in ABAQUS
解决uniapp等富文本图片宽度溢出
Stm32cube infrared remote control: input capture
Headline article_ signature
Automatic conversion and cast
Leetcode daily practice - the number of digits in the offer 56 array of the sword finger
Microsoft question 100 - do it every day - question 16
KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书
Reset grafana login password to default password
后台弹出layer提示
2021-04-02
Sort 2 bubble sort and quick sort (recursive and non recursive explanation)
ANSYS secondary development - MFC interface calls ADPL file
Baidu editor ueeditor, when editing too much content, the toolbar is not visible, which is not convenient for editing or uploading problems
Microsoft question 100 - do it every day - question 11
"Wei Lai Cup" 2022 Niuke summer multi school training camp 3 acfhj