Skip to content

Allow multiple wildcard vals #14906

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

Merged
merged 1 commit into from
Apr 13, 2022
Merged

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Apr 11, 2022

Allow multiple definitions of the form

val _: T = e

in a scope. Translate them by changing the _ to fresh
wildcardParamNames.

Allow multiple definitions of the form

    val _: T = e

in a scope. Translate them by changing the `_` to fresh
wildcardParamNames.
@som-snytt
Copy link
Contributor

I see Scala 2

class C { val _ = 42 ; val _: Int = 27 ; val _: String = Array(17) }

is

          <synthetic> <artifact> private[this] val x$1 = 42: @scala.unchecked match {
            case _ => ()
          };
          <synthetic> <artifact> private[this] val x$2 = 27: @scala.unchecked match {
            case (_: Int) => ()
          };
          <synthetic> <artifact> private[this] val x$3 = Array(17): @scala.unchecked match {
            case (_: String) => ()
          }

so

       error: scrutinee is incompatible with pattern type;
        found   : String
        required: Array[Int]

instead of

       error: type mismatch;
        found   : Array[Int]
        required: String

I don't know what the expectation was, but I would expect val _ = Array() to keep a reference to my allocation. I can't tell if that is a bug or feature without a coin, and who carries coins any more?

@odersky
Copy link
Contributor Author

odersky commented Apr 11, 2022

@som-snytt I'd say the Scala 3 error message is preferable.

Copy link
Contributor

@julienrf julienrf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks good to me.

@julienrf julienrf assigned odersky and unassigned julienrf Apr 12, 2022
@som-snytt
Copy link
Contributor

BTW, after trying it out, I see the Scala 2 behavior is the same except for that one error message.

My point was actually that I would expect

val _ = new Thing

to keep my Thing from getting garbage-collected. But I guess that qualifies as neither a feature nor a bug.

@odersky odersky merged commit fcf62e1 into scala:main Apr 13, 2022
@odersky odersky deleted the change-wildcard-vals branch April 13, 2022 08:27
@Kordyjan Kordyjan added this to the 3.2.0 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants