以下函数参数签名的含义是什么:((int?) - >单元)? = null?
我有一个具有多个参数的函数。这些参数之一是
private fun mySpecialFunction(
variable1: Int,
onChanged: ((Int?) -> Unit)? = null
)
在函数中被调用,就像:
onChanged?.invoke(2)
在上部呼叫站点上一样,它被称为:
mySpecialFunction(
variable1 = 1,
onChanged = {
// do something with the number invoked above
}
)
在kotlin中调用的onchange的用法如何?
I have a function with several parameters. One of these paramters is
private fun mySpecialFunction(
variable1: Int,
onChanged: ((Int?) -> Unit)? = null
)
Later in the function it is invoked like:
onChanged?.invoke(2)
And on the upper calling site, it is called like:
mySpecialFunction(
variable1 = 1,
onChanged = {
// do something with the number invoked above
}
)
How is this usage of onChange called in Kotlin?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
on Changed
是一个无效的“ noreferrer”>“ noreferrer”>高级订购 函数。?
在“((int?) - > unit)?
”中表示on Changed
的值可以为null。另外,已分配了默认值null
,这意味着,如果您不传递此参数的值,则将为null。高阶函数的通常语法是
(args) - > returnType
。在您的情况下,on Changed
将无效的int(int?
)作为参数,并且不会返回任何内容,即返回单位
。on Changed?.invoke(2)
表示,如果on Changed
不是null,请调用并通过2
作为参数。onChanged
is a nullable higher-order function. The?
in "((Int?) -> Unit)?
" means that the value ofonChanged
can be null. Also, it has been assigned the default valuenull
which means that if you don't pass a value for this parameter, it will be null.The usual syntax for a higher order function is
(Args) -> ReturnType
. In your case,onChanged
takes an nullable Int (Int?
) as an argument and does not return anything i.e. returnsUnit
.onChanged?.invoke(2)
means that ifonChanged
is not null, invoke it and pass2
as the argument.