Kotlin Reference (七) Returns and Jumps
most from reference
kotlin有三个结构跳跃表达式
- return 默认情况下,从最近的封闭函数或匿名函数返回。
- break 跳出整个循环
- continue 跳出本次循环,进行下一次循环
所有这些表达式都可以用作更大表达式的一部分:
val s = person.name ?: return
这些表达式的类型是Nothing类型
Break and Continue
kotlin中的任何表达式都可以标记一个标签。标签是带有@标志的标识符,例如:abc@,fooBar@是有效的标签(参见语法)。为了标注表达式,我们只是在前面放上一个标签:
loop@ for (i in 1..100) {
// ...
}
现在,我们可以为braak和continue添加label:
loop@ for (i in 1..100) {
for (j in 1..100) {
if (...) break@loop
}
}
break是跳出整个循环,continue是跳出本次循环,继续下一次循环。
Return
使用函数,局部函数和对象表达式,函数可以嵌套到kotlin中。允许我们从外部函数返回。最重要的作用是从lambda表达式返回,如下:
fun foo() {
ints.forEach {
if (it == 0) return // nonlocal return from inside lambda directly to the caller of foo()
print(it)
}
}
return表达式从最近的封闭函数返回,即foo
(请注意,这种非本地返回仅适用于传递给内联函数的lambda表达式),如果要从lambda表达式返回,我们必须对其进行标记并限定返回值:
fun foo() {
ints.forEach lit@ {
if (it == 0) return@lit
print(it)
}
}
现在,它只从lambda表达式返回。通常使用implicits标签更方便,这样的标签与传递lambda的函数具有相同的名称:
fun foo() {
ints.forEach {
if (it == 0) return@forEach
print(it)
}
}
或者,我们可以用匿名函数替换lambda表达式。一个匿名函数的return语句从匿名函数本身返回。
fun foo() {
ints.forEach(fun(value: Int) {
if (value == 0) return // local return to the caller of the anonymous fun, i.e. the forEach loop
print(value)
})
}
当返回一个值时,解析器会给出返回值,即
return@a 1
表示”返回1标签@”,而不是”返回标记表达式(@a 1)”
Kotlin Reference (七) Returns and Jumps的更多相关文章
- Kotlin Reference (十二) Extensions
most from reference Kotlin与C#和Gosu类似,提供了扩展一个新功能的类,而不必继承类或使用任何类型的设计模式,如Decorator(装饰者模式).这是通过称为扩展的特殊声明 ...
- Kotlin Reference (十一) Visibility Modifiers
most from reference 类,对象,接口,构造函数,函数,属性及setters具有可见性修饰符(getter总是具有和属性一样的可见性).在kotlin中油4个可视化修饰符:privat ...
- Kotlin Reference (十) Interfaces
most from reference 接口 Kotlin中的接口非常类似于Java8,它们可以包含抽象方法的声明以及方法实现.与抽象类不同的是接口不能存储状态.它们可以具有属性,但这些需要是抽象的或 ...
- Kotlin Reference (九) Properties and Fields
most from reference 声明属性 Koltin的类都有属性,这些属性可以声明为可变的,使用var关键字或用val关键字生声明不可变属性. class Address { var nam ...
- Kotlin Reference (八) Classes and Objects
most from reference 类 Kotlin的类的声明使用关键字class class Invoice { } 类声明由类名.类头(指定其类型参数,构造函数等)和类体组成,由大括号括起来. ...
- Kotlin Reference (六) Control Flow
most from reference if表达式 在kotlin中,if是一个表达式,即它返回一个值.kotlin中没有Java中的三元运算符. // Traditional usage var m ...
- Kotlin Reference (五) Packages
most from reference 包 源文件可以从包声明开始: package foo.bar fun baz() {} class Goo {} // ... 源文件的所有内容(如类和函数)都 ...
- Kotlin Reference (四) Basic Types
most from reference 基本类型 在kotlin中,一切都是对象,我们可以在任何变量上调用成员函数和属性.一些类型可以具有特殊的内部表示:例如,数字.字符和布尔值都可以在运行时被表示为 ...
- Kotlin Reference (三) Coding Conventions
most from reference 命名规则 1.使用驼峰式命名规则,尽量避免在命名中使用下划线 2.类型以大写字母开头 3.方法和属性以小写字母开头 4.使用4个空格缩进 5.public的方法 ...
随机推荐
- SSH三大框架简介
我们知道,传统的Java Web应用程序是采用JSP+Servlet+Javabean来实现的,这种模式实现了最基本的MVC分层,使的程序结构分为几层,有负责前台展示的 JSP.负责流程逻辑控制的Se ...
- vector vector int 初始化
方法一: vector<vector<int>>array=(2,vector<int>()); array[0].push_back(1); array[i].p ...
- 1970年1月1日(00:00:00 GMT)Unix 时间戳(Unix Timestamp)
转载自(http://jm.ncxyol.com/post-88.html) 今天在看Python API时,看到time模块: The epoch is the point where the ...
- RedHat7.4最小化安装没有ifconfig命令
软件环境 VirtualBox 5.2.8 rhel-server-7.4-x86_64-dvd.iso 系统环境 Win10 64 位 8G内存 最小化安装了RedHat7.4之后,进入系统之后使用 ...
- LeetCode—— Partition Equal Subset Sum
Question Given a non-empty array containing only positive integers, find if the array can be partiti ...
- 从0开始 图论学习 邻接表 STL vector
邻接表表示 用vector实现 writer:pprp 代码如下: #include <bits/stdc++.h> using namespace std; const int maxn ...
- arm-linux-gcc安装使用教程
arm-linux-gcc如何下载安装2(转) [转]ubuntu下交叉编译环境构建(arm-linux-gcc-3.4.1.tar.bz2 ) 2009-03-03 10:05 1.下载arm-li ...
- imagemagick在windows下安装(转,有改动)
原文地址:http://blog.csdn.net/royal_coffee/article/details/1602933 注意:本補述僅提供 Windows 下安裝建議. 1. 到 http:// ...
- cygwin下安装软件
cygwin下安装软件cygwin工具安装新的软件和常见的命令windows8.1下安装Cygwin并通过apt-cyg安装软件包Cygwin利用apt-cyg安装gcc.g++.make和gdb 首 ...
- w3c标准盒模型与IE传统模型的区别
一.盒子模型(box model) 在HTML文档中的每个元素被描绘为矩形盒子.确定其大小,属性——比如颜色.背景.边框,及其位置是渲染引擎的目标. CSS下这些矩形盒子由标准盒模型描述.这个模型描述 ...