kotlin内联函数_Kotlin内联函数,参数化

kotlin 内联函数In this tutorial, we’ll be looking into Kotlin inline function. We’ll follow that with Reified Type Parameters. 在本教程中,我们将研究Kotlin内联函数。 我们将在后面加上Reified Type Parameters。 Kotlin内联函数 (Kotlin ...
摘要由CSDN通过智能技术生成

kotlin内联函数

In this tutorial, we’ll be looking into Kotlin inline function. We’ll follow that with Reified Type Parameters.

在本教程中,我们将研究Kotlin内联函数。 我们将在后面加上Reified Type Parameters。

Kotlin内联函数 (Kotlin inline functions)

We’ve discussed Kotlin Higher Order Functions and Lambda Expressions before.
They’re super useful in passing functions as parameters. But these functions are objects which have there own callbacks and subsequently memory allocations. Let’s understand how these functions passed as parameters internally work through an example.

我们之前已经讨论过Kotlin高阶函数和Lambda表达式
在将函数作为参数传递时,它们非常有用。 但是这些函数是具有自己的回调以及随后的内存分配的对象。 让我们通过示例了解如何将这些函数作为参数传递给内部。

fun main(args: Array<String>) {

    println("Hey how are you doing")
    sampleFunction("JournalDev.com", ::println)
}

fun sampleFunction(str: String, expression: (String) -> Unit) {
    println("This is Kotlin Inline Functions Tutorial")
    expression(str)
}

sampleFunction when run passes println as the parameter. Now Kotlin is a JVM based language so everything is converted into bytecode. Let’s look at the bytecode of the above code by going to Tools | Kotlin | Show Bytecode.

运行时的sampleFunctionprintln作为参数传递。 现在Kotlin是一种基于JVM的语言,因此所有内容都将转换为字节码。 让我们通过转到“ 工具” |“工具栏” |查看上面代码的字节码。 Kotlin| 显示字节码

The main part expression.invoke(). Invoking the lambda expression (println) would create an additional call and hence memory. The invoke method looks like this in Java:

主要部分是expression.invoke()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值