A question about use 'lowerThan' in 'precedencegroup', maybe is a bug

I hope I'm wrong. I turned to developer.apple.com, a people suggested me: Could be worth a bug report on documentation. But I still want to confirm, and I don’t know how to correctly submit the bug report on documentation.

Summary:
I see the ' Improved operator declarations ', try the example:

// module Swift
precedencegroup Additive { higherThan: Range }
precedencegroup Multiplicative { higherThan: Additive }

// module A
precedencegroup Equivalence {
  higherThan: Comparative
  lowerThan: Additive  // possible, because Additive lies in another module
}

but the Range and Comparative show error:

Unknown precedence group.

So I add it:

precedencegroup Range { }
precedencegroup Comparative { }

Now, lowerThan: Additive show error:

Precedence group cannot be given lower precedence than group in same module; make the other precedence group higher than this one instead.

So,I try make Range or Comparative higherThan any precedencegroup I know, both too.
And then, I try change Range to RangeFormationPrecedence, Comparative to ComparisonPrecedence, it's error too.
But this is no error:

precedencegroup TestPrecedence {
     higherThan: ComparisonPrecedence
     lowerThan: RangeFormationPrecedence
 }

I tried everything I could think of, I cann't make a precedencegroup lowerThan custom precedencegroup.

What you are reading from is not documentation of how to use Swift at present, but rather leftover documents from its design process. They are retained for historical purposes, but are not updated to match any changes that have been made to the language since.

For up‐to‐date documentation, look on the Swift or Apple websites instead of in the Swift Evolution repository.

Since that document was written, these had their names changed to RangeFormationPrecedence, AdditionPrecedence, MultiplicationPrecedence and ComparisonPrecedence.

The comment was meant to indicate that these lines would be part of the standard library included with Swift itself. You were not expected to copy and paste them into your own file.

This part labelled “module A” is what would belong in your own code if you wanted it. It should still work, as long as you update Comparative to ComparisonPrecedence

Once the two separate halves of the example are placed where they were intended to be, then Equivalence is indeed in a separate module as the comment describes.

Always preferring higherThan reduces the odds of circularity mistakes. Within one module you control both declarations, and so it is simplest to declare all relationships using higherThan.

When you refer to a precedence group from another module, you do not necessarily have access to its code, and may be unable to alter it. So for that type of relationship, lowerThan is allowed so that you can describe the relationship from within your own module’s code.

(If you are not already familiar with what a module is, see here and here.)

This is illegal, unless the argument to lowerThan is from another module (i.e. coming from an import statement).

1 Like
Terms of Service

Privacy Policy

Cookie Policy