Here's an unofficial overview of how the names seem to have come to be.
让我们
让我们
的灵感来自函数式编程世界.根据Wikipedia
"让我们"表达式将函数定义与受限范围相关联
在像Haskell这样的FP语言中,可以使用让我们
将值绑定到受限范围内的变量,如下所示
aaa = 让我们 y = 1+2
z = 4+6
in y+z
Kotlin中的等效(尽管过于复杂)代码是
fun aaa() = (1+2).让我们 { y ->
(4+6).让我们 { z ->
y + z
}
}
The typical usage of 让我们
is to bind the result of some computation to a scope without "polluting" the outer scope.
creater.createObject().让我们 {
if (it.isCorrect && it.shouldBeLogged) {
logger.log(it)
}
}
// `it` is out of scope here
with
The with
function is inspired by the with
language construct from languages like Delphi or Visual Basic (and probably many others) where
The with keyword is a convenience provided by Delphi for referencing
elements of a complex variable, such as a record or object.
myObject.colour := clRed;
myObject.size := 23.5;
myObject.name := 'Fred';
可以重写:
with myObject do
begin
colour := clRed;
size := 23.5;
name := 'Fred';
end;
The equivalent Kotlin would be
with(myObject) {
color = clRed
size = 23.5
name = "Fred"
}
申请
申请
was added to the stdlib relatively late in the milestone phase (M13). You can see this question from 2015 where a user asks for exactly such a function and even suggests the later to-be-used name "申请".
在第https://youtrack.jetbrains.com/issue/KT-6903期和第https://youtrack.jetbrains.com/issue/KT-6094期中,你可以看到有关命名的讨论.提出了build
和init
等备选方案,但丹尼尔·沃多潘提出的申请
最终获胜.
申请
is similar to with
in that it can be used to initialize objects outside of the constructor. That's why, in my opinion, 申请
might as well be named with
. However as with
was added to the stdlib first, the Kotlin devs decided against breaking existing code and added it under a different name.
Ironically, the language Xtend provides the so-called with-operator =>
which basically does the same as 申请
.
还包括
还包括
被添加到stdlib中,甚至比申请
还要晚,即在1.1版中.同样,https://youtrack.jetbrains.com/issue/KT-6903包含了讨论.该函数基本上类似于申请
,只是它采用常规的lambda (T) -> Unit
,而不是扩展lambda T.() -> Unit
.
提议的名称包括"申请It"、"申请让我们"、"on"、"Tap"、"touch"、"peek"、"make".但"也"获胜了,因为它不会与任何关键字或其他stdlib函数冲突,而且它的用法(或多或少)读起来像英语句子.
Example
val object = creater.createObject().还包括 { it.initiliaze() }
读起来有点像
Creater,创建对象并初始化它!
其他用法读起来有点像英语句子的stdlib函数包括takeIf
和takeUnless
,它们也是在1.1版中添加的.
跑
Finally, the 跑
function actually has two signatures. The first one fun <R> 跑(block: () -> R): R
simply takes a lambda and 跑s it. It is mostly used for assigning the result of a lambda expression to a top-level property
val logger = 跑 {
val name = System.property("logger_name")
Logger.create(name)
}
第二签名fun <T, R> T.跑(block: T.() -> R): R
是以扩展λ为参数的扩展函数,并且由于对称原因似乎也被命名为"Run".它也"运行"lambda,但在扩展接收器的上下文中
val result = myObject.跑 {
intitialize()
computeResult()
}
我不知道这个命名有任何历史原因.