Uploaded image for project: 'Swift'
  1. Swift
  2. SR-7131

You cannot assign an operator without parens the way you can use an operator in map

    XMLWordPrintable

    Details

      Description

      This is legal: 

       

      let (x, y) = ("Hello.", "There.")

      zip(x, y).map(==)

      zip(x, y).map(((==)))

       

      This is not:

       

      public typealias Fooquatable = (Foo, Foo) -> Bool

      let c: Fooquatable = == // unary operator cannot be separated from its operand

       

      To compile you must surround the == with parens

      let c: Fooquatable = (==)

       

      The fixit is inappropriate (it just deletes the new line and any trailing comment), and the reason appears to be the way assignment tokens expect unary operators to operate, even though this is an infix operator:

       

      if (OperEndLoc == Tok.getLoc())
      diagnose(PreviousLoc, diag::expected_expr_after_unary_operator);
      else
      diagnose(PreviousLoc, diag::expected_prefix_operator)
      .fixItRemoveChars(OperEndLoc, Tok.getLoc());

       

      It is not overly burdensome to use parens, but it should be made clear that this is the necessary use, not just in direct assignment but when creating collections of operators, for example:

       

      let j: [Fooquatable] = [<=, ==, >=] // expected expression after unary operator

       

      whereas parenthesizing each operator works properly.

       

      I'd also like to see documentation that explains that operator implementations are sugar and are not static members. They do not appear in a mirror reflecting SomeType.self.

       

       

       

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              erica erica sadun
            • Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated: