I have a question about the generated Optional opt...
# arrow
q
I have a question about the generated Optional optics for nullable fields. The laws for affine traversals state that "If you get nothing when extracting a value, then whatever you put in, the operation is no-op" (http://oleg.fi/gists/posts/2017-04-18-glassery.html#laws:affine-traversal). The generated code that I see for a nullable field is setting the value without checking if a value is present. Here's an example:
Copy code
inline val TestOptics.Companion.nullable: arrow.optics.Optional<TestOptics, Int> inline get()= arrow.optics.Optional(
  getOrModify = { testOptics: TestOptics -> testOptics.`nullable`?.right() ?: testOptics.left() },
  set = { testOptics: TestOptics, value: Int -> testOptics.copy(`nullable` = value) }
)
Is the arrow Optional optic not equivalent to an affine traversal? If not, what is it equivalent to?
n
I believe it is the same as an AffineTraversal.
q
If it is the same, then the generated code is incorrect. Calling set on an option where the focus is null should be a no-op
s
cc\\ @Alejandro Serrano Mena
For an
Optional
I think this is indeed correct. That sounds like the behavior of a
Lens
for a nullable field.