-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Invent given pattern name in for comprehension #23121
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
base: main
Are you sure you want to change the base?
Conversation
def4dd7
to
4d0f402
Compare
1df2c1d
to
c9a9dbc
Compare
c9a9dbc
to
26578ca
Compare
@sjrd Minimized commit! |
26578ca
to
71280c8
Compare
I knew I'd fixed the spelling of |
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.
I think this will break scenarios where there are two given
captures for two types where inventGivenName
produces the same name. For example a given foo: List[Option[Int]]
and a given bar: List[Option[String]]
.
We should at the very least add a test for such a situation, make sure that both can still be resolved.
@sjrd I considered that because I just noted elsewhere (#23832 (comment)) that local givens can have clashing names but could just as well have fresh names (modulo code can refer to the invented name). I'll add |
Actually, as shown in the
because
It progresses to clashing locals.
But it would be best if the additional help from "double definition" were available for these cases. (Because givens are so mysterious? Because anonymous givens are common and fragile?) |
71280c8
to
52832f8
Compare
Maybe there is a way to name for given j: Int <- 1 to 2 yield j or something like this? I failed to find a way to set dedicated name for a |
@som-snytt thank you so much! I couldn't imagine such a way to write it - even after using Only place where I found such usage is Pattern-Bound Given Instances, but I didn't imagine that it could be used in for-comprehension too. Sadly there are zero examples for such use-case in docs. And IDEA doesn't provide hint to change from: for {
//...
given X = getX()
x = summon[X]
//.. to for {
//...
x @ given X = getX()
//.. Honestly, I don't know many language users, who would go through the syntax summary. |
@mr-git thanks, I'd forgotten the name for it, but that was the answer to "what is the replacement for That is, I found an example #12676 My point is that it is a FAQ but maybe not written up in a helpful document. I don't see it in the migration guide (at a glance). |
Fixes #23119
A given pattern in a for comprehension results in a fresh val in the body of the mapping function, but it should have the same (arbitrary) name as in the rest of the expansion.
This commit gives the given its usual given name (in
makeIdPat
) so that the unused check can check it.Currently, without
-preview
, showing that$1$
is calledgiven_Int
by typer:Without
-preview
, this commit:With
-preview
, the tupling map is eliminated early: