Skip to content
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

Shouldn't match be right-associative? #7819

Closed
scabug opened this issue Sep 7, 2013 · 3 comments
Closed

Shouldn't match be right-associative? #7819

scabug opened this issue Sep 7, 2013 · 3 comments

Comments

@scabug
Copy link

scabug commented Sep 7, 2013

Currently the operand on the lhs of match is left-associative. Thus we are forced to use parenthesis around the entire match block, e.g. as follows.

def f(a:String, b:B) = a + (b match {
   case Thing => "thing"
   case Thang => "thang"
})

The closing parenthesis straddles such a large distance far from the original point-of-concern. Rather if match was right-associative, then in the opposing case, the parenthesis are local, e.g. as follows.

```scala
def f(a:String, b:String) = (a + b) match {
   case "thing" => Thing
   case "thang" => Thang
}

Or the operand could be put on the rhs of match.

P.S. I tried to post this as a new topic at scala-debate using Google Groups, yet my new topic was automatically deleted within a few seconds of posting it. I tried 3 times. I have no problem replying to existing topics there. I don't know how to report that problem. So I am filing a report here. Note I am unable to change the "Blocker" Priority setting on this bug.

@scabug
Copy link
Author

scabug commented Sep 7, 2013

Imported From: https://issues.scala-lang.org/browse/SI-7819?orig=1
Reporter: Shelby Moore III (shelby)

@scabug
Copy link
Author

scabug commented Sep 8, 2013

@retronym said:
Please try again on the mailing list to see if you can find the history of this choice. [scala-language] would be suitable.

I don't know myself, but if I had to guess I would say it would be so you could reason about the associativy by thinking of match as a regular method:

object Test {
  def `match`(a: PartialFunction[Any, Any]) = 0

  def op(t: Test.type) = t

  this op this match { case x => x }

  this op this `match` { case x => x }
}
qbin/scalac -Xprint:parser sandbox/test.scala
[[syntax trees at end of                    parser]] // test.scala
package <empty> {
  object Test extends scala.AnyRef {
    def <init>() = {
      super.<init>();
      ()
    };
    def `match`(a: PartialFunction[Any, Any]) = 0;
    this match {
      case (x @ _) => x
    };
    def op(t: Test.type) = t;
    this.op(this) match {
      case (x @ _) => x
    };
    this.op(this).`match`(<empty> match {
      case (x @ _) => x
    })
  }
}

@scabug scabug closed this as completed Sep 8, 2013
@scabug
Copy link
Author

scabug commented Sep 10, 2013

Shelby Moore III (shelby) said (edited on Sep 10, 2013 7:05:40 PM UTC):
One thing I really like about getting rid of the ternary operator ( ? : ), that the silent bug of not enclosing in paired parenthesis the first operand or the entire ternary (when you need it) is avoided. The first operand comes after the if and when employed as an operand itself, an if-else clause is forced to be enclosed in parenthesis by the grammar. Unfortunately match has the first problem of the ternary (but not the second due to the required braces).

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

2 participants