-
-
Notifications
You must be signed in to change notification settings - Fork 203
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
Add withDefault and atOrElse to replace None by a default value #886
Conversation
Should this go into the |
Very good question. In theory, yes, but I am tempted to propose to soften our policy with regards to "lawfullness". I mean we should definitely document when an optic or combinator is a bit dodgy and maybe use a prefix/suffix to make it clear, but if it is really useful and the criteria for "lawfullness" are most often satisfied in real-world cases, then I think we should offer it. That's just my opinion, what do you think? and @yilinwei ? |
Haskell lens has a similar but slightly different solution: https://hackage.haskell.org/package/lens-4.19.2/docs/Control-Lens-Combinators.html#v:non (if that linking doesn't work properly, the function you are looking for is eg (super useful for nested maps)
They similarly note that it's not necessarily lawful ( The nested map situation in particular is why I'm SUPER keen on a solution to this :) |
@TimWSpence correct, I believe I used the name |
Oh sorry @julien-truffaut, I only looked at |
Sorry, didn't see this earlier. I'm personally quite lax about coherence breakages in my own code (I've overridden an We should definitely make a note of the problems in the comment - but the any unexpected breakages I suspect is where people will property test the optic rather than anything else. I'm ambivalent on the whether to have a prefix/suffix. |
I added tests and documentation for |
|
||
checkAll("fromIso", AtTests[MMap[Int, String], Int, Option[String]]) | ||
|
||
checkAll("ListMap", AtTests[ListMap[Int, String], Int, Option[String]]) | ||
|
||
test("atOrElse") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be nice to have a test for nested maps? Obviously it should just work but given that it's one of the primary motivations for this, it might be nice to test it explicitly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, this might be better in AtExample
if that is considered documentation of how to use it. Usage of at
for nested maps is definitely not completely obvious at first
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make sense. I am thinking to get rid of example
module and instead use the website. It is probably easier to access.
@@ -21,6 +23,36 @@ abstract class At[S, I, A] extends Serializable { | |||
trait AtFunctions { | |||
def at[S, I, A](i: I)(implicit ev: At[S, I, A]): Lens[S, A] = ev.at(i) | |||
|
|||
/** | |||
* Creates a Lens that zooms into an index `i` inside `S`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It really is a pity that there isn't a better way to link documentation in scala. These examples feel as though they belong elsewhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
This is a slightly unlawful
Iso
but it is very convenient when you want to zoom into a Map like structure andset/modify
the value when the key doesn't exist.