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
Synthesize a PartialFunction from function literal #8172
Conversation
Hum, can I ask why? |
Sure! Because it's safe and the workaround is annoying (ironically the reverse, being able to define a function from case statements, is unsafe but already possible). |
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.
Looking good. Suggested a few improvements/tweaks.
5182656
to
d3da7eb
Compare
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.
Nice! Few more suggestions.
d3da7eb
to
64039d3
Compare
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.
Looks good!
64039d3
to
77c1342
Compare
(Added some overloading and eta-expanding tests, which came up in conversation.) |
77c1342
to
ecb5264
Compare
Anyone else want to review this? |
It's not too late to add language in 6.23.1 of the spec. There's already a slot for PF because of |
ecb5264
to
ced240b
Compare
Good thinking. I actually dropped the "is of the shape", seeing as it's no longer true in 2.13, right?
|
It's the other case you preserve in your patch:
So the last is still false not true with this change. A couple of paragraphs later in the spec says how the PF is constructed. I want to suggest it's the same as 8.5 for "pattern matching anonymous functions", the bare case block. The piece to add is how a default case is supplied with the function body, so that isDefinedAt is true. (Edit: maybe don't use those words.) The list of "expected types" can now just say PF without the conditional; you were right to drop it there, but add it back in the subsequent paragraph: If it has this shape, isDefinedAt is derived from the cases; otherwise, isDefinedAt is true. (I think of the translation as if it were written as for 8.5, which is how it's implemented, but the spec is only in terms of adding the isDefinedAt member.) |
ced240b
to
76ab674
Compare
Thanks for your feedback, @som-snytt. Let me know if I captured everything you were indicating or if I missed or misunderstood anything - I'm happy to make further changes. |
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.
Sorry, I intended to come up with some draft words this weekend. But it was the weekend.
Here is my simplification, which says that your PR is the usual case, but in this special case where the body looks like a "pattern matching anonymous function", it's handled in a similar way. I wasn't gutsy enough to say that directly, because it would require linking to that section. (Maybe this text should add, See [link to anchor for PMAFs].)
When a PartialFunction
is required, an additional member isDefinedAt
is synthesized, which simply returns true
.
However, if the function literal has the shape x => x match { $\ldots$ }
,
then isDefinedAt
is derived from the pattern match in the following way:
each case from the match expression evaluates to true
,
and if there is no default case,
a default case is added that evalutes to false
.
76ab674
to
d411969
Compare
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.
Still LGTM :-) I think we can merge.
In some places it's misspelled Funtion1 |
#8430 (with an unintentional typo in the commit message. English is hard.) |
Allow
val pf: PartialFunction[Int, Int] = { _ => 3 }