
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
83 lines
2.7 KiB
Plaintext
83 lines
2.7 KiB
Plaintext
== Why is this an issue?
|
|
|
|
It is acceptable to override standard operators to provide appropriate behaviors for your classes. But it is not appropriate to change those operators' associativity or precedence from the standard. Doing so will inevitably lead to misuse and mistakes for users of the class.
|
|
|
|
|
|
Instead of overriding an existing operator's associativity or precedence, you should either let them use the default values or define a completely new operator.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,swift]
|
|
----
|
|
infix operator - : CustomAdditionPrecedence // Noncompliant. For a different behavior create a different operator
|
|
|
|
precedencegroup CustomAdditionPrecedence {
|
|
associativity: right
|
|
}
|
|
|
|
func - (lhs: MyInt, rhs: MyInt) -> MyInt {
|
|
// ...
|
|
}
|
|
|
|
var a = MyInt(10), b = MyInt(5), c = MyInt(5)
|
|
print(a - b - c) // against expectations, this outputs 10
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,swift]
|
|
----
|
|
infix operator <- : CustomAdditionPrecedence
|
|
|
|
precedencegroup CustomAdditionPrecedence {
|
|
associativity: right
|
|
}
|
|
|
|
func <- (lhs: MyInt, rhs: MyInt) -> MyInt {
|
|
// ...
|
|
}
|
|
|
|
var a = MyInt(10), b = MyInt(5), c = MyInt(5)
|
|
|
|
var a = MyInt(10), b = MyInt(5), c = MyInt(5)
|
|
print(a - b - c) // prints 0 as expected
|
|
print(a <- b <- c) // prints 10
|
|
----
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Choose another operator symbol.
|
|
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
=== relates to: S993
|
|
|
|
=== on 17 Jun 2015, 13:10:54 Ann Campbell wrote:
|
|
\[~elena.vilchik] I know you'll probably want to update the code samples. I included the ones here as a starting point.
|
|
|
|
=== on 17 Jun 2015, 16:53:00 Elena Vilchik wrote:
|
|
\[~ann.campbell.2] please, have a look at code examples
|
|
|
|
=== on 17 Jun 2015, 18:18:52 Ann Campbell wrote:
|
|
\[~elena.vilchik], assuming I'm reading this right, the Noncompliant and Compliant samples show completely different operators. The Compliant Solution needs to be a "fixed" version of the Noncompliant Example, not completely different code that happens to pass.
|
|
|
|
=== on 18 Jun 2015, 07:14:22 Elena Vilchik wrote:
|
|
\[~ann.campbell.2] that exactly what i was trying to do :) if you want operator which subtracts values but has right associativity, then create your custom operator ("<-" in my example). For custom operator it's required to declare function which defines its action (there is no need for such function for standard operators). And we need another message, not clause should be removed, but whole statement. May be "Remove this operator declaration."?
|
|
|
|
=== on 18 Jun 2015, 15:19:42 Ann Campbell wrote:
|
|
after our collaboration [~elena.vilchik], I think we're both happy.
|
|
|
|
endif::env-github,rspecator-view[]
|