如何在Kotlin中内联扩展功能的接收器参数? [英] How can I inline the receiver parameter of an extension function in Kotlin?

查看:101
本文介绍了如何在Kotlin中内联扩展功能的接收器参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题的启发,我在想一个人如何内联扩展功能的接收器参数?从理论上讲,像这样:

Inspired by this question, I was thinking about how could one inline the receiver parameter of an extension function? In theory, like this:

inline fun <T> not(crossinline predicate : (T) -> Boolean)
    = { e : T -> !predicate(e) }

只需predicate成为我们的接收器功能:

Just that predicate becomes our receiver function:

operator inline fun <T> ((T) -> Boolean).not()
    = { e : T -> !this(e) }

现在,对于上面的代码,我希望编译器抱怨它需要crossinline;但是,我收到以下警告:

Now, for the above code, I'd expect the compiler to complain that it needs crossinline; however, I get the following warning:

警告:内联public inline operator fun <T> ((T) -> Boolean).not(): (T) -> Boolean的预期性能影响微不足道.内联最适合具有功能类型参数的功能

Warning: Expected performance impact of inlining public inline operator fun <T> ((T) -> Boolean).not(): (T) -> Boolean is insignificant. Inlining works best for functions with parameters of functional types

这让我相信编译器没有内联该函数的接收者.添加inlinecrossinline仅会产生语法错误.

This lets me believe that the compiler is not inlining the receiver of that function. Adding inline or crossinline only produces syntax errors.

无法内联第二个函数会降低第一个函数的性能.

Not being able to inline the second function decreases the performance over the first one.

有什么办法告诉编译器内联该接收者参数吗?

Is there any way to tell the compiler to inline that receiver parameter?

推荐答案

https中进行了跟踪://youtrack.jetbrains.com/issue/KT-5837 ,但似乎并未积极地进行:(我实际上

This is tracked in https://youtrack.jetbrains.com/issue/KT-5837, but doesn't seem to be actively worked on :( I actually just suggested supporting noinline/crossinline hoping that's the main reason receiver isn't inlined.

这篇关于如何在Kotlin中内联扩展功能的接收器参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆