https://kotlinlang.org logo
#arrow
Title
# arrow
d

dave08

10/25/2023, 11:51 AM
@Alejandro Serrano.Mena I'm very confused... I have say
@optics data class Foo(val bar: SomeEnum?)
and if I run
Foo.bar.set(Foo(null), SomeEnum.baz)
it gets set properly, whereas if I run
Every.list<Foo>().bar.set(listOf(Foo(null)), SomeEnum.baz)
it stays null... is there any way to get this to work (set it to baz...)?
It seems like the extension
bar
being generated is using
Option
, so the behaviour makes sense, the question is if there's some way around this...
I need it to be set even when it's null (I guess that's a prism, but how should that work with Every?
a

Alejandro Serrano.Mena

10/25/2023, 12:16 PM
in principle in 1.2.x a nullable field generates an Optional, which you cannot set from
null
to not-null... 🤔
d

dave08

10/25/2023, 12:25 PM
Any way to work aound this?
Like maybe focusing on the Option with a Prism
I didn't really expect that behaviour from Every... maybe index() yes, but Every?
a

Alejandro Serrano.Mena

10/25/2023, 12:31 PM
in principle this comes from the behavior of
Foo.bar
, I'm very surprised that
Foo.bar.set(Foo(null), SomeEnum.baz)
actually sets the
null
d

dave08

10/25/2023, 12:33 PM
It's also funny that even that companion object in an Optional...
It's composing the same companion object Optional that was generated (changed to remove private code):
Copy code
public inline val Foo.Companion.bar: arrow.optics.Optional<Foo, SomeEnum> inline get() = arrow.optics.Optional(
  getOrModify = { foo: Foo -> foo.`bar`?.right() ?: foo.left() },
  set = { foo: Foo, value: SomeEnum -> foo.copy(`bar` = value) }
)
But when composed with Every, it just doesn't set those nulls...
a

Alejandro Serrano.Mena

10/25/2023, 12:38 PM
interesting... that code is actually not what it should, the
set
should only set when not null
d

dave08

10/25/2023, 12:40 PM
I triple checked, it DOES set it...
But not when composed with Every... 😞
a

Alejandro Serrano.Mena

10/25/2023, 12:42 PM
in theory in 1.2.x nullable fields should work like the version composed with Every, regardless of whether they're composed or not
this is a bug then, but I'm not sure whether the potential breakage from people relying on it is worth it, with 2.0 fixing the entire hierarhy
d

dave08

10/25/2023, 12:44 PM
I wish there was a version of this that would set it even with Every... will there be such a thing in 2.0? How could I write such a thing now?
a

Alejandro Serrano.Mena

10/25/2023, 12:45 PM
yes, this is the fix we made; even for nullable fields you can set
d

dave08

10/25/2023, 12:45 PM
Any way to do it now?
a

Alejandro Serrano.Mena

10/25/2023, 12:47 PM
you'd need to write the optics by hand
d

dave08

10/25/2023, 12:49 PM
A Prism? A Lens? I'm not quite sure what would compose with Every...
a

Alejandro Serrano.Mena

10/25/2023, 12:49 PM
you'd need to create a
Lens<Foo, SomeEnum?>
(instead of
Optional<Foo, SomeEnum>
, notice the
?
in the lens)
and then write an extension
Copy code
val <A> Every<A, Foo>.bar: Every<A, SomeEnum?> = this + Foo.barAsLens
👍🏼 1
d

dave08

10/25/2023, 12:57 PM
That worked, thanks!
Maybe this should be noted somewhere... this problem caused my unit tests to fail for a while, until I finally figured out that optics was causing my test setup to be invalid...
Is 2.0 coming out anywhere in the near future, maybe some kind of ETA for it?
a

Alejandro Serrano.Mena

10/25/2023, 1:02 PM
our intention is still to release it when Kotlin 2.0 releases -- this is why we added compilation to K2 to the pipeline
once there's a release date for Kotlin 2.0, we'll decide whether it makes sense for us to release earlier
d

dave08

10/25/2023, 1:02 PM
The reworking of optics is already done, or just planned?
a

Alejandro Serrano.Mena

10/25/2023, 1:03 PM
it's done in the
arrow-2
branch
d

dave08

10/25/2023, 1:04 PM
But arrow-2 is probably not a version to be used in production with Kotlin 1.9.10 right?
a

Alejandro Serrano.Mena

10/25/2023, 1:05 PM
it shouldn't break or anything, but we provide only snapshots
our plan is to release a "final 1.2.x" after 1.9.20 comes out, and then make
arrow-2
the "main" branch, then we should start releasing alphas
d

dave08

10/25/2023, 1:07 PM
And the "final 1.2.x" won't be back-porting any of the optics reworking, it'll just be deprecating a few more things to get closer to 2.0?
a

Alejandro Serrano.Mena

10/25/2023, 1:07 PM
yep
d

dave08

10/25/2023, 1:08 PM
Makes us even more eager to finally get Kotlin 2.0... 😁...
I guess meanwhile I'll have to stick with 1.2.x and all these workarounds...
s

sindrenm

10/25/2023, 1:15 PM
We haven't even been able to move to 1.2.x because of https://github.com/arrow-kt/arrow/issues/3134.