-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Pattern given
on the LHS of a val
definition should enter corresponding scope
#11897
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
Comments
def test = {
val (x, given Int) = (1, 1)
summon[Int]
} desugars to the following: def test: <error no implicit values were found that match type Int> =
{
val x: Int =
Tuple2.apply[Int, Int](1, 1):(Int, Int) @unchecked match
{
case Tuple2.unapply[Int, Int](x @ _, given given_Int @ _:Int) =>
x:Int
}
summon[Int](/* missing */summon[Int])
} |
Yes, but that's totally unexpected from a user point of view. I always thought the desugaring of local The correct approach would be to desugar them as: def test = {
(1, 1) match
case (x, given Int) =>
summon[Int]
... // rest of the block
} Though this does not really generalize to member |
sorry I should clarify, I agree that alongside |
Note: this would also mean that toplevel vals and object vals cannot be allowed to bind patterns. So a rather drastic change. |
Yes, it would definitely be too drastic to disallow them now. So at least member BTW, the problem in this issue is reminiscent of the limitations of type binding in val (ls: List[t]) = List(1, 2, 3) // Not found: type t
List(1, 2, 3) match { case ls: List[t] => ... } // works as expected |
for reference, this behaviour was intentional from when given patterns were first added |
In the discussion at the dotty meeting we decided to resolve this with an error for given patterns in val definitions |
I tried to exploit the desugaring as shown, for example an extractor in the pattern takes the given, but it's not in scope. If nothing else, closing off this avenue will save folks many hours trying to pursue it. |
Compiler version
3.0.0-RC1
Minimized code
Expectation
This is quite surprising/unexpected.
I would expect it to work in the same way as the following:
The text was updated successfully, but these errors were encountered: