Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Composing Optional with Iso and Prism prevents inserting value #301

Closed
Odomontois opened this issue Nov 26, 2015 · 2 comments
Closed

Composing Optional with Iso and Prism prevents inserting value #301

Odomontois opened this issue Nov 26, 2015 · 2 comments

Comments

@Odomontois
Copy link

Some person here asks for help
If I'm not misundersanding index should work as upsert already and does not work because of composition.

Composing Optional regarding that with anything from Iso and up to Optional is happening like

def composePrism[C, D](other: PPrism[A, B, C, D]): POptional[S, T, C, D] =
    composeOptional(other.asOptional)

setting new value in this composition is like

def set(d: D): S => T =
        self.modify(other.set(d))

modify for Optional will modify only existent field so if we compose Optionalwith anything else, it becomes update only, it could not insert

while for PPrism we could use another definition like

  def set(d: D): S => T =
    self.set(other.reverseGet(d))

which able to insert new value even if original object has nothing here yet

So can we have specialized compose for Iso and Prism ?

@Odomontois Odomontois changed the title Composed Optional stops from inserting value Composing Optional with Iso and Prism prevents inserting value Nov 26, 2015
@Odomontois Odomontois changed the title Composing Optional with Iso and Prism prevents inserting value Composing Optional with Iso and Prism prevents inserting value Nov 26, 2015
@julien-truffaut
Copy link
Member

Thank you very much @Odomontois for raising the question. I answered in SO regarding this particular example.

Regarding the general question, it is expected that you cannot insert or delete a value in an Maplike data structure with an Optional. Here is why:

def index(key: K): Optional[Map[K, V], V]

so index creates a Optional between a Map[K, V] and a specific value at index key with type V. Here is the type signature of set and modify:

def set(v: V)(m: Map[K, V]): Map[K, V]
def modify(f: V => V)(m: Map[K, V]): Map[K, V]

set can be seen as a weaker modify:

def set(v: V)(m: Map[K, V]): Map[K, V] = modify(_ => v)(m)

Now we can see why we cannot insert or delete via index, we only have a function V => V. In order to insert or delete we need a function Option[V] => Option[V]

@Odomontois
Copy link
Author

Ok. It's clear now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants