当前位置:网站首页>【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大厂面试题(有解析)

领取地址:

边栏推荐
- 【Kotlin学习】类、对象和接口——带非默认构造方法或属性的类、数据类和类委托、object关键字
- LeetCode 535. Encryption and decryption of tinyurl
- [point cloud processing paper crazy reading cutting-edge version 12] - adaptive graph revolution for point cloud analysis
- Low code momentum, this information management system development artifact, you deserve it!
- AcWing 785. 快速排序(模板)
- LeetCode 715. Range 模块
- Computing level network notes
- 【点云处理之论文狂读前沿版10】—— MVTN: Multi-View Transformation Network for 3D Shape Recognition
- MySQL installation and configuration (command line version)
- Save the drama shortage, programmers' favorite high-score American drama TOP10
猜你喜欢
![[point cloud processing paper crazy reading classic version 8] - o-cnn: octree based revolutionary neural networks for 3D shape analysis](/img/fa/36d28b754a9f380bfd86d4562268c3.png)
[point cloud processing paper crazy reading classic version 8] - o-cnn: octree based revolutionary neural networks for 3D shape analysis
![[point cloud processing paper crazy reading classic version 10] - pointcnn: revolution on x-transformed points](/img/c1/045ca010b212376dc3e5532d25c654.png)
[point cloud processing paper crazy reading classic version 10] - pointcnn: revolution on x-transformed points

即时通讯IM,是时代进步的逆流?看看JNPF怎么说

On February 14, 2022, learn the imitation Niuke project - develop the registration function

Excel is not as good as jnpf form for 3 minutes in an hour. Leaders must praise it when making reports like this!

低代码起势,这款信息管理系统开发神器,你值得拥有!

LeetCode 1089. Duplicate zero
![[point cloud processing paper crazy reading frontier version 8] - pointview gcn: 3D shape classification with multi view point clouds](/img/ee/3286e76797a75c0f999c728fd2b555.png)
[point cloud processing paper crazy reading frontier version 8] - pointview gcn: 3D shape classification with multi view point clouds

In the digital transformation, what problems will occur in enterprise equipment management? Jnpf may be the "optimal solution"

Vs2019 configuration opencv3 detailed graphic tutorial and implementation of test code
随机推荐
Tag paste operator (#)
[graduation season | advanced technology Er] another graduation season, I change my career as soon as I graduate, from animal science to programmer. Programmers have something to say in 10 years
Introduction to the usage of getopts in shell
LeetCode 438. Find all letter ectopic words in the string
【毕业季|进击的技术er】又到一年毕业季,一毕业就转行,从动物科学到程序员,10年程序员有话说
Methods of checking ports according to processes and checking processes according to ports
状态压缩DP AcWing 291. 蒙德里安的梦想
State compression DP acwing 291 Mondrian's dream
DOM render mount patch responsive system
The difference between if -n and -z in shell
Shell script kills the process according to the port number
Linxu learning (4) -- Yum and apt commands
On February 14, 2022, learn the imitation Niuke project - develop the registration function
LeetCode 515. Find the maximum value in each tree row
LeetCode 324. Swing sort II
Using variables in sed command
Digital management medium + low code, jnpf opens a new engine for enterprise digital transformation
Sword finger offer II 029 Sorted circular linked list
2022-2-13 learn the imitation Niuke project - Project debugging skills
Just graduate student reading thesis