以下函数参数签名的含义是什么:((int?) - >单元)? = null?

发布于 2025-02-08 23:58:28 字数 415 浏览 2 评论 0原文

我有一个具有多个参数的函数。这些参数之一是

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

洛阳烟雨空心柳 2025-02-15 23:58:28

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作为参数。

mySpecialFunction(
    variable1 = 1, 
    onChanged = {
        // This section will get executed when the onChanged function is invoked. Here the passed `Int?` will be available as `it`
    }
)

onChanged is a nullable higher-order function. The ? in "((Int?) -> Unit)?" means that the value of onChanged can be null. Also, it has been assigned the default value null 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. returns Unit.

onChanged?.invoke(2) means that if onChanged is not null, invoke it and pass 2 as the argument.

mySpecialFunction(
    variable1 = 1, 
    onChanged = {
        // This section will get executed when the onChanged function is invoked. Here the passed `Int?` will be available as `it`
    }
)
国粹 2025-02-15 23:58:28
private fun mySpecialFunction(
    variable1: Int,
    // define nullable higher-order function with default parameter `null`
    onChanged: ((Int?) -> Unit)? = null
)

// nullsafe call && lamda invoke
onChanged?.invoke(2)

mySpecialFunction(
    variable1 = 1, 
    // define onChange lambda
    onChanged = {
    }
)

// equal to this
interface ChangeCallback {
    fun onChange(int: Int?)
}

fun mySpecialFunction(
    variable1: Int,
    onChange: ChangeCallback? = null
)

private fun mySpecialFunction(
    variable1: Int,
    // define nullable higher-order function with default parameter `null`
    onChanged: ((Int?) -> Unit)? = null
)

// nullsafe call && lamda invoke
onChanged?.invoke(2)

mySpecialFunction(
    variable1 = 1, 
    // define onChange lambda
    onChanged = {
    }
)

// equal to this
interface ChangeCallback {
    fun onChange(int: Int?)
}

fun mySpecialFunction(
    variable1: Int,
    onChange: ChangeCallback? = null
)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文