使用反射来传递和修改图元而不使用数组 [英] Using reflection to pass and modify a primitive without using array
问题描述
在imgui 端口上工作时,我一直被用作原始数组来模拟c ++指针/地址通过.
Working on a imgui port, I've been always used primitive arrays to simulate c++ pointers/addresses pass.
例如:
checkbox("Anti-aliased lines", bool.apply { set(0, style.antiAliasedLines) })
style.antiAliasedLines = bool[0]
其中bool: BooleanArray
我只是探讨了直接传递字段的可能性,例如c ++:
I just explored the possibility to directly pass the field instead, sort of c++:
ImGui::Checkbox("Anti-aliased lines", &style.AntiAliasedLines);
kotlin:
checkbox("Anti-aliased lines", style::antiAliasedLines })
我只需要在checkbox()
上容纳一个相应的KMutableProperty0<Boolean>
,我还仔细检查了是否可以在同一函数中设置该字段:有一个get(): Boolean
和set(Boolean)
I just needed to accomodate a corresponding KMutableProperty0<Boolean>
on checkbox()
, and I also double checked to have the possibility to set that field within the same function: there is a get(): Boolean
and a set(Boolean)
我担心的是:
-
与Java配合使用会很好吗?
does this play nice with java?
如果我沿着这条路线走,我应该注意哪些禁忌症?诸如表演之类的?
are there any contraindications I should be aware of, if I go down this route? Such as performances or whatever?
因为我有一种感觉,因为这种方法有点笨拙和肮脏
Because I got the feeling that since this approach is somehow hacky and dirty
推荐答案
与Java配合使用会很好吗?
Does this play nice with java?
一件行不通的事是引用Kotlin用来表示Java getter和setter的合成属性,例如getFoo
+ setFoo
→foo
(并且不能使用绑定的可调用引用bar::foo
).目前尚不支持.
One thing that won't work is referencing synthetic properties that Kotlin uses to represent Java getters and setters, e.g. getFoo
+ setFoo
→ foo
(and you cannot use a bound callable reference bar::foo
). This is not supported for now.
您可以尝试通过自定义KMutableProperty0
实现来解决该问题,该实现接受对Bar::getFoo
,Bar::setFoo
和bar
的未绑定可调用引用,并在获取和设置其值的意义上充当属性引用. /p>
You can try to workaround that by a custom KMutableProperty0
implementation that accepts unbound callable references to Bar::getFoo
, Bar::setFoo
and bar
and acts as a property reference in the sense of getting and setting its value.
如果我沿着这条路线走,我有什么禁忌症要注意吗?诸如表演之类的?
Are there any contraindications I should be aware of, if I go down this route? Such as performances or whatever?
绑定的可调用引用拥有对接收方的引用,因此style::antiAliasedLines
将使style
与GC隔离,并且您必须注意存储哪些可调用引用.
A bound callable reference holds a reference to the receiver, so style::antiAliasedLines
will keep style
from GC, and you have to be careful about which callable references you store.
这篇关于使用反射来传递和修改图元而不使用数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!