当前位置:网站首页>【Kotlin学习】高阶函数的控制流——lambda的返回语句和匿名函数
【Kotlin学习】高阶函数的控制流——lambda的返回语句和匿名函数
2022-07-03 08:59:00 【安卓老猴子】
高阶函数的控制流
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核心知识点,以及一些最新的大厂面试题、知识脑图和视频资料解析。
需要的小伙伴直接点击文末小卡片免费领取哦,以后的路也希望我们能一起走下去。(谢谢大家一直以来的支持,需要的自己领取)
Android学习PDF+架构视频+面试文档+源码笔记
部分资料一览:
- 330页PDF Android学习核心笔记(内含8大板块)


- Android学习的系统对应视频

- Android进阶的系统对应学习资料

- Android BAT大厂面试题(有解析)

领取地址:

边栏推荐
- C language file reading and writing
- Severity code description the project file line prohibits the display of status error c2440 "initialization": unable to convert from "const char [31]" to "char *"
- What is an excellent fast development framework like?
- LeetCode 532. 数组中的 k-diff 数对
- Using DLV to analyze the high CPU consumption of golang process
- LeetCode 438. 找到字符串中所有字母异位词
- Noip 2002 popularity group selection number
- LeetCode 75. Color classification
- LeetCode 75. 颜色分类
- Use of sort command in shell
猜你喜欢

LeetCode 75. Color classification

Jenkins learning (I) -- Jenkins installation

Vscode connect to remote server

LeetCode 1089. Duplicate zero
![[advanced feature learning on point clouds using multi resolution features and learning]](/img/f0/abed28e94eb4a95c716ab8cecffe04.png)
[advanced feature learning on point clouds using multi resolution features and learning]

Memory search acwing 901 skiing

Sword finger offer II 091 Paint the house

教育信息化步入2.0,看看JNPF如何帮助教师减负,提高效率?

我们有个共同的名字,XX工

Pic16f648a-e/ss PIC16 8-bit microcontroller, 7KB (4kx14)
随机推荐
C language programming specification
Common penetration test range
Linxu learning (4) -- Yum and apt commands
[point cloud processing paper crazy reading classic version 11] - mining point cloud local structures by kernel correlation and graph pooling
The method of replacing the newline character '\n' of a file with a space in the shell
【点云处理之论文狂读前沿版12】—— Adaptive Graph Convolution for Point Cloud Analysis
[point cloud processing paper crazy reading frontier version 10] - mvtn: multi view transformation network for 3D shape recognition
LeetCode 715. Range module
Severity code description the project file line prohibits the display of status error c2440 "initialization": unable to convert from "const char [31]" to "char *"
【Kotlin学习】类、对象和接口——带非默认构造方法或属性的类、数据类和类委托、object关键字
Vscode connect to remote server
Jenkins learning (III) -- setting scheduled tasks
Complex character + number pyramid
LeetCode 515. 在每个树行中找最大值
Debug debugging - Visual Studio 2022
State compression DP acwing 291 Mondrian's dream
LeetCode 508. 出现次数最多的子树元素和
On February 14, 2022, learn the imitation Niuke project - develop the registration function
Too many open files solution
AcWing 786. Number k