https://kotlinlang.org logo
#announcements
Title
# announcements
a

aballano

10/15/2019, 12:42 PM
Hi there, I'm trying to port some usages of AtomicInteger/Boolean/Reference to use github.com/Kotlin/kotlinx.atomicfu instead but apparently this is failing due to
factory kotlinx.atomicfu.AtomicFU::atomic is used outside of constructor or class initialisation
I'm just using them in local functions mainly, is this not permitted? if so, is there an alternative usage to do so? Thanks in advance! cc @elizarov
e

elizarov

10/15/2019, 7:59 PM
Why would you need to use them in local functions? (yes, it is not supported)
a

aballano

10/15/2019, 8:01 PM
I got into thinking they'll work as a 1:1 mapping to Atomics from java
is there a specific technical decission for this to be like this? mostly curious now
s

simon.vergauwen

10/15/2019, 8:03 PM
We have that use-case in a lot places in Arrow. Custom stream concurrency operators for example.
☝️ 1
e

elizarov

10/15/2019, 8:08 PM
Cleate an explicit container class to keep your state.
They are not 1-on-1 mapping to atomic. They are mapping to a volatile field +
AtomicXxxFieldUpdater
(hence the name
AtomicFU
). This is more efficient (no boxes for atomics)
s

simon.vergauwen

10/15/2019, 8:09 PM
Okay, I see. So to gain that power we need to manually wrap it.
e

elizarov

10/15/2019, 8:10 PM
Yes. Or send PR to support your use-case. Don’t foreget to write tests, though 😉
s

simon.vergauwen

10/15/2019, 8:10 PM
If there is any interest for such a wrapper I’d be happy to create a PR with tests!
Thanks for the help
e

elizarov

10/15/2019, 8:11 PM
For wrapper — no. But support using them as locals — why not.
👍 2
a

aballano

10/15/2019, 8:23 PM
thank you @elizarov
28 Views