myriad icon indicating copy to clipboard operation
myriad copied to clipboard

Lens generator: implement prisms for multi case discriminated unions

Open travis-leith opened this issue 4 years ago • 8 comments

The way Aether deals with this is to assume the following optics are defined

type MyUnion =
    | First of int
    | Second of string

    (* Prism<MyUnion,int> *)
    static member First_ =
        (fun m ->
            match m with 
            | First i -> Some i
            | _ -> None),
        (fun i m ->
            match m with
            | First _ -> First i
            | m -> m)


If Myriad could generate these automatically for me, along with https://github.com/MoiraeSoftware/myriad/issues/103, then I think it completes support for Aether.

travis-leith avatar Apr 22 '21 15:04 travis-leith

For the avoidance of doubt, this does not create a dependency on Aether, but it is complimentary.

travis-leith avatar Apr 22 '21 15:04 travis-leith

I have no objections about having these as an option in the config.

7sharp9 avatar Apr 23 '21 11:04 7sharp9

Would love to see this. Unfortunately unable to help out as I have no capacity to ramp up on new codebases now.

cmeeren avatar Apr 02 '22 12:04 cmeeren

I’m not sure how much prisms are used, I don’t use them myself so the use case is lost on me, and the enthusiasm to implement it.

Sent from my iPhone

On 2 Apr 2022, at 13:35, Christer van der Meeren @.***> wrote:

 Would love to see this. Unfortunately unable to help out as I have no capacity to ramp up on new codebases now.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.

7sharp9 avatar Apr 03 '22 14:04 7sharp9

I understand.

I by no means expect the below to change anything, but I want to mention it anyway, for the record if nothing else.

As far as motivation goes, implementing this would constitute a "completion" as it were of Myriad's existing optics functionality.

As for an example use case, imagine you have:

type FirstData = { Field1: ...; ... }

type MyUnion = First of FirstData | ...

type Foo = { Union: MyUnion option; ... }

Now imagine you have an instance of Foo and you want to change the value of the deeply nested Field1 (if Foo.Union is Some and First is FirstData).

With suitably generated lenses, this is as simple as

let updatedFoo = foo |> set (Foo.union_ >?> MyUnion.first_ >?> FirstData.field1_) newValue

Without this, you can probably imagine how to do this using normal copy-and-update syntax, pattern matching, etc. You would have many nested pattern matches and copy-and-update expressions.

Lenses of course scale very well to arbitrary deep hierarchies and composes very well, so that Myriad can generate simple lenses for each type separately, and then code like the above can be used throughout.

cmeeren avatar Apr 03 '22 19:04 cmeeren

If I were to make a PR for this (no promises; more or less requires #145 and #146 for the interest to be there), would I make the changes in Myriad.Plugins/LensesGenerator.fs? I assume that since Myriad already supports lenses, this would make the most sense. (Another alternative is a third-party plugin.)

cmeeren avatar May 07 '22 15:05 cmeeren

It would make sense to add the prism feature to the lenses plug-in here. The lens plugin was entirely submitted by a third party I’ve just added things like the aether pipeline friendly mode and some code clean up to meet my style guide it’s not quite there yet in that regard though. 😊

On Sat, 7 May 2022 at 16:47, Christer van der Meeren < @.***> wrote:

If I were to make a PR for this (absolutely no promises), would I make the changes in https://github.com/MoiraeSoftware/myriad/blob/3b1861e9feee3db1321b32983496788d61f6ea20/src/Myriad.Plugins/LensesGenerator.fs? I assume that since Myriad already supports lenses, this would make the most sense. (Another alternative is a third-party plugin.)

— Reply to this email directly, view it on GitHub https://github.com/MoiraeSoftware/myriad/issues/107#issuecomment-1120230811, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAEPXSTPXUPOWRGRZXRDLWDVI2F7TANCNFSM43MYB32Q . You are receiving this because you commented.Message ID: @.***>

7sharp9 avatar May 09 '22 12:05 7sharp9

Thanks! For the record, note that I edited my previous comment to link to two other issues, #145 and #146, which would need to be supported before I will be interested in using Myriad and therefore extending the lenses functionality.

cmeeren avatar May 09 '22 12:05 cmeeren