Using setl and view for the same optic in one function gives error "The type Data.Identity does not match the type Data.Const"
See original GitHub issueI’m trying to use optics to set a record field together with the record’s UpdatedAt: DateTimeOffset
field only if the field value has changed.
I have created the following generic function, which doesn’t compile:
let inline private setField valueOptic value updatedAtOptic now source =
let oldVal = view valueOptic source
if value = oldVal then source
else
setl valueOptic value source
|> setl updatedAtOptic now
What am I doing wrong?
Issue Analytics
- State:
- Created 3 years ago
- Comments:23 (21 by maintainers)
Top Results From Across the Web
Error: The property '#/settings/X/variables/X' of type object ...
When I try to import a Guide theme, I get the following error: Failed to import theme. The property '#/settings/X/variables/X' of type object ......
Read more >Optic API for Multi-Model Data Access
This data access function is helpful for testing and debugging. Build a table with two rows and return the row that matches the...
Read more >Fixing common type problems
This section shows how to fix some of the errors and warnings you might see from the analyzer or an IDE. Static analysis...
Read more >IBM Informix Messages and Corrections
Earlier versions of the database server do not support raw tables. ... the code in a license-file line does not match the other...
Read more >stat()--Get File Information
The stat() function gets status information about a specified file and places it in the area of memory pointed to by the buf...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
We can consider using those functors for lensing but of course, we’ll need to make sure all other optics don’t break. Testing should be added although there is some lens coverage already, but we’ll need to add test for stuff that was previously not allowed, like your use case. Feel free to open PR or a draft PR to play with it.
We can’t fix type system limitations (like rank-n types) but we can certainly explore tricks like the suggested one that makes a better user experience.
TBH I’m not an expert in lenses, it is an abstraction that it’s not specific to this library or to F# so I would try to find guidance in other places as well, for instance I asked your question in the FP slack, in the #Lens channel as I’m also curious to see if/how lens would help in that situation.
My feeling is that we need a lens that encapsulate that behavior, we can certainly create it like this:
but I understand that you want to derive it in a general way, and I think that would involve using other functors in anycase. So if you’re happy with the mutable solution stick to it. I personally like it because yes it does a mutation but it’s encapsulated inside the function, it doesn’t leak outside, as opposed to the solution of passing twice the lens.
A similar technical problem was reported by @reinux on our gitter 2 weeks ago:
The fix suggested by @nikolamilekic should apply here as well.