当前位置:网站首页>【Android Kotlin】lambda的返回语句和匿名函数
【Android Kotlin】lambda的返回语句和匿名函数
2022-07-04 12:33:00 【不秃头de程序猿】
lambda中的返回语句:从一个封闭的函数返回
在一个普通循环中使用return
在传递给forEach的lambda中使用return
如果你在lambda
中使用return
关键字,他会从调用lambda的函数中返回,并不只是从lambda返回。这样的return语句叫作非局部返回,因为它从一个比包含return的代码块更大的代码块中返回了。我们可以类比在java中的for循环和synchronized代码块,显然会从函数中返回
要注意只有在以lambda作为参数的函数是内联函数的时候才能从更外层的函数返回。上图forEach的函数体和lambda的函数体一起被内联了,所以在编译的时候很容易做到从包含它的函数中返回。在一个非内联函数的lambda中使用return是不允许的。一个非内联函数可以把传给它的lambda保存在变量中,以便在函数返回以后可以继续使用
从lambda返回:使用标签返回
可以在lambda表达式中使用局部返回,lambda中的局部返回跟for循环中的break表达式相似。它会终止lambda的执行,并接着从调用lambda的代码处执行。要区分局部返回和非局部返回,需要用到标签。想从一个lambda表达式返回你可以标记它,然后在return关键字后引用这个标签
要标记一个lambda表达式,在lambda的花括号之前放一个标签名,接着放一个@
符号。要从一个lambda返回,在return关键字后放一个@符号,再加上标签名
另一种是使用lambda作为参数的函数的函数名可以作为标签,比如forEach
如果你显式地指定了lambda表达式的标签,再使用作为标签没有任何效果,一个lambda表达式的标签数量不能多于一个
带标签的this表达式
如果你给带接收者的lambda指定标签,就可以通过对应的带有标签的this
表达式访问它的隐式接收者
匿名函数:默认使用局部返回
匿名函数看起来和普通函数相似
匿名函数和普通函数由相同的指定返回值类型的规则,代码块体匿名函数需要显式指定返回类型,如果使用表达式函数体就可以省略返回类型
使用表达式函数体匿名函数
在匿名函数中,不带标签的return表达式会从匿名函数返回,而不是从包含匿名函数的函数返回。
规则:return从最近的使用fun
关键字声明的函数返回。lambda没有使用fun关键字,所以lambda中的return从最外层的函数返回,而匿名函数使用了fun。尽管匿名函数看起来跟普通函数很相似,但它其实是lambda表达式的另一种语法形式而已
最后
在技术领域内,没有任何一门课程可以让你学完后一劳永逸,再好的课程也只能是“师傅领进门,修行靠个人”。“学无止境”这句话,在任何技术领域,都不只是良好的习惯,更是程序员和工程师们不被时代淘汰、获得更好机会和发展的必要前提。
**如果你觉得自己学习效率低,缺乏正确的指导,可以扫码,加入我们资源丰富,学习氛围浓厚的技术圈一起学习交流吧!
加入我们吧!里面有许多来自一线的技术大牛,也有在小厂或外包公司奋斗的码农,我们致力打造一个平等,高质量的Android交流圈子,不一定能短期就让每个人的技术突飞猛进,但从长远来说,眼光,格局,长远发展的方向才是最重要的。
边栏推荐
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 6
- LVS load balancing cluster deployment - Dr direct routing mode
- MySQL advanced (Advanced) SQL statement
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 10
- Tableau makes data summary after linking the database, and summary exceptions occasionally occur.
- C language: find the palindrome number whose 100-999 is a multiple of 7
- C语言数组
- Argminer: a pytorch package for processing, enhancing, training, and reasoning argument mining datasets
- Review of week 278 of leetcode II
- 《天天数学》连载57:二月二十六日
猜你喜欢
Source code analysis of the implementation mechanism of multisets in guava class library
Azure solution: how can third-party tools call azure blob storage to store data?
When synchronized encounters this thing, there is a big hole, pay attention!
The frost peel off the purple dragon scale, and the xiariba people will talk about database SQL optimization and the principle of indexing (primary / secondary / clustered / non clustered)
C language array
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 17
C语言函数
13、 C window form technology and basic controls (3)
Method of setting default items in C # ComboBox control code
SAP ui5 date type sap ui. model. type. Analysis of the display format of date
随机推荐
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 17
R语言--readr包读写数据
What if the chat record is gone? How to restore wechat chat records on Apple Mobile
Openssl3.0 learning 20 provider KDF
Workplace liquor bureau must pay attention to
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 7
Global and Chinese market of dental elevators 2022-2028: Research Report on technology, participants, trends, market size and share
When synchronized encounters this thing, there is a big hole, pay attention!
【数据聚类】第四章第一节3:DBSCAN性能分析、优缺点和参数选择方法
Abnormal mode of ARM processor
C language: find the length of string
[data clustering] section 3 of Chapter 4: DBSCAN performance analysis, advantages and disadvantages, and parameter selection methods
MySQL advanced review
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 11
Tableau makes data summary after linking the database, and summary exceptions occasionally occur.
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 13
Paper notes ACL 2020 improving event detection via open domain trigger knowledge
C语言:围圈报号排序问题
[directory] search
Hongke case study on storm impact in coastal areas of North Carolina using lidar