Skip to content

Spec: Are blocks allowed in patterns? #1774

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

Closed
liufengyun opened this issue Dec 5, 2016 · 7 comments
Closed

Spec: Are blocks allowed in patterns? #1774

liufengyun opened this issue Dec 5, 2016 · 7 comments

Comments

@liufengyun
Copy link
Contributor

liufengyun commented Dec 5, 2016

Scalac doesn't parse following programs syntactically, but support it if it's synthesized.

Dotty accepts the code syntactically, but the typer generates a typing error.

object Test {
  val a = (3, 4) match {
    case ({name: Int}, {parent: Int}) => (name, parent)
  }

  val name = a._1
  val parent = a._2
}

Error message:

-- Error: examples/block.scala -------------------------------------------------
6 |  val name = a._1
  |             ^
  |             cyclic reference involving value a
-- Error: examples/block.scala -------------------------------------------------
7 |  val parent = a._2
  |               ^
  |               cyclic reference involving value a
-- Error: examples/block.scala -------------------------------------------------
3 |    case ({name: Int}, {parent: Int}) => (name, parent)
  |                                          ^^^^
  |                                        cyclic reference involving value a

three errors found
@julienrf
Copy link
Contributor

julienrf commented Dec 5, 2016

Support could be useful for this kind of stuff:

(3, 4) match {
  case { val single = MyTupleExtractor(Snd); single(4) } => …
}

(It makes it possible to manipulate extractors in rhs position, for instance to configure them)

@julienrf
Copy link
Contributor

julienrf commented Dec 5, 2016

But I’m not sure it is worth it… :)

@liufengyun
Copy link
Contributor Author

liufengyun commented Dec 5, 2016

The implementation of Quasiquote in scala.meta (link) relies on anonymous extractors like follows:

          new {
            def unapply(input: Joy) = {
              ..$preamble
              input match {
                case $lifted => $thenp
                case _       => $elsep
              }
            }
          }.unapply(..$args)

which gets expanded to a block:

      {
          class $anon {
            def unapply(input: Joy) = {
              ..$preamble
              input match {
                case $lifted => $thenp
                case _       => $elsep
              }
            }
          }

         new $anon
      }.unapply(..$args)

@odersky
Copy link
Contributor

odersky commented Dec 11, 2016

I think it would be good to allow this! Needs a spec change, though.

@liufengyun
Copy link
Contributor Author

As a side note, we don't need this feature in order to implement Quasiquote in dotty. The quasi quote in Dotty now pass all regression set in scalameta.

@liufengyun
Copy link
Contributor Author

Close this for now, I don't think it's a good idea to have blocks in patterns, the code is not readable.

@propensive
Copy link
Contributor

It seems like it would open up a lot of new and interesting possibilities, though I'm a little scared about what should and shouldn't be allowed...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants