it'd would be nice if I can set the value by simpl...
# stdlib
e
it'd would be nice if I can set the value by simply calling
ref(value)
instead
ref.set(value)
g
what about get? Looks not symmetric
l
@elect
var myProperty by ref; myProperty = value
, works on local
val
too
e
@gildor Actually you can use
get()
or
invoke()
@louiscad I'll play with that
g
You can, but
ref()
and
ref(value)
look to cryptic and not clear for me, not like getter and setter, I would try to use local delegate instead, or just get and set methods
e
think about
Copy code
val a = ref()
ref(b)
@louiscad it complains about missing
getValue()
and
setValue()
l
@elect You can write a custom delegate with
operator fun
g
Yes, sure, I understand how it would work and this is inconsistent with other Kotlin stdlib code. I think it's kinda abuse of invoke overloading, but I think it can be fine for your own project if you use this a lot, but too much for stdlib
e
@louiscad looking at custom delegates, https://try.kotlinlang.org/#/Examples/Delegated%20properties/Custom%20delegate/Custom%20delegate.kt, this seems involving inevitably a new
class
instantiation, am I right?
@gildor yeah, ok
g
@elect No, it’s not how you should implement it. Just implement getValue and setValue as extensions of KMutableProperty0, I will show you an example
This version allows to use KMutableProperty0 as delegate (top level, class level or local), no additional classes created, and even operator can be inlined
Updated sample with example of delegating property to another property
e
thanks both
cool Andrey, anyway my case is slightly more complicate because I have a common generic path valid for different types of `Number`:
Int
,
Float
,
Double
and
Long
. I also have to figure it out how to deal with that..
g
Could you show an example? Just curious what do you want to achieve. Because this delegate should works with any generic type
e
it seems is not, here for example
inputScalar
can be called by
inputInt/Float
, https://github.com/kotlin-graphics/imgui/blob/master/src/main/kotlin/imgui/imgui/widgets%20inputKeyboard.kt#L145
that based on the
dataType
will cast the value accordingly, perform the operation and return the result
I'm using simply a generic
Copy code
inline operator fun <R> KMutableProperty0<R>.setValue(host: Any?, property: KProperty<*>, value: R) = set(value)
inline operator fun <R> KMutableProperty0<R>.getValue(host: Any?, property: KProperty<*>): R = get()
also, I just found out that increment operator doesnt work with that
if I have
KMutableProperty0<Char>
and
var c by ref
and then
c += 'a'
this is not working, incremental operation is not supported
but this is a minor problem
g
Your problem is wildcard in KMutableProperty0<*>, of cours you cannot use it in a type safe way, you can introduce another implementation, that returns
Any
instead of particular class
e
which implementation you mean?
g
I mean your code uses wildcard:
dataPtr: KMutableProperty0<*>
Increment works perfectly for me
without any problems, including char
Augmented assignment and increment are not supported for local delegated properties and inline properties
oh I see,I tried with toplevel
e
Im on IDEA 2018.1.5, kt 1.2.50
ah, that's why
g
yes, I see what you mean
I just tried with top level one
e
yeah, I read one moment later
g
e
yes sir 😎
also
Copy code
fun collapsingHeader(label: String, openPtr: KMutableProperty0<Boolean>?, flags_: TreeNodeFlags = 0): Boolean {

        var open by openPtr
In this case I would like to
by?
idea reports it as
Boolean
g
You can write extension for nullable KMutableProperty0 and handle nullable case