Postfix operators that start with ? or !


(Toni Suter) #1

Hi,

In Swift 3, I can declare a postfix operator that starts with a ? or a !, but as far as I can tell, there's no way to actually use such an operator:

postfix operator ?**
postfix func ?**(x: Int) -> Int { return x }
let x = 12
x?** // error: use of unresolved operator '**'

postfix operator !**
postfix func !**(x: Int) -> Int { return x }
let x = 12
x!** // error: use of unresolved operator '**'

It makes sense to me, why that happens. There's no way for the parser to know, whether ?** is the end of an optional chaining expression
followed by a postfix operator ** or whether it's a single postfix operator ?**. Similarly, !** could refer to a forced-value expression followed
by a postfix operator ** or a single postfix operator !**.

Even with semantic knowledge, it is unclear, what should happen. For example, in the following situation either parsing decisions would
result in a valid program:

postfix operator ++
postfix operator ?++
postfix func ++(x: inout Int) {
    x += 1
}
postfix func ?++(x: Int?) -> Int? {
    return x
}
var x: Int? = 0
x?++ // what should happen here? Swift 3 chooses optional chaining expression followed by ++ operator

I think it would probably be best to disallow postfix operators that start with a question mark or an exclamation mark.
At the moment it is possible to declare them, but not to use them. That's not very user-friendly.

By the way, infix operators have a similar problem. Infix operators that start with a ? or a ! must be surrounded by whitespace whereas
other infix operators don't have to be:

let x: Int? = 0
x ?? 10 // fine
x??0 // error
2 + 10 // fine
2+10 // fine

I think this is less of an issue, because you can at least use such infix operators if you add the necessary whitespace.

What do you think about this? Would it make sense to disallow postfix operators that start with a question mark or an exclamation mark?

Thanks and best regards,
Toni


(Alex Martini) #2

In Swift 3, I can declare a postfix operator that starts with a ? or a !, but as far as I can tell, there's no way to actually use such an operator:

I think it would probably be best to disallow postfix operators that start with a question mark or an exclamation mark.
At the moment it is possible to declare them, but not to use them. That's not very user-friendly.

This looks like a bug — the documentation and the behavior disagree. The docs say:

Although you can define custom operators that contain a question mark (?), they can’t consist of a single question mark character only. Additionally, although operators can contain an exclamation mark (!), postfix operators cannot begin with either a question mark or an exclamation mark.

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/doc/uid/TP40014097-CH30-ID418

···

On Nov 1, 2016, at 7:23 AM, Toni Suter via swift-evolution <swift-evolution@swift.org> wrote:


(Toni Suter) #3

Oh, I didn't see that, thanks! I'll write a bug report.

···

Am 01.11.2016 um 17:52 schrieb Alex Martini <amartini@apple.com>:

On Nov 1, 2016, at 7:23 AM, Toni Suter via swift-evolution <swift-evolution@swift.org> wrote:

In Swift 3, I can declare a postfix operator that starts with a ? or a !, but as far as I can tell, there's no way to actually use such an operator:

I think it would probably be best to disallow postfix operators that start with a question mark or an exclamation mark.
At the moment it is possible to declare them, but not to use them. That's not very user-friendly.

This looks like a bug — the documentation and the behavior disagree. The docs say:

Although you can define custom operators that contain a question mark (?), they can’t consist of a single question mark character only. Additionally, although operators can contain an exclamation mark (!), postfix operators cannot begin with either a question mark or an exclamation mark.

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/doc/uid/TP40014097-CH30-ID418


(Anton Zhilin) #4

Hello Tony,
Feel free to file a bug report. If there are no spaces between operator
characters, then they should be parsed as a single operator.


(Toni Suter) #5

I have filed a bug: https://bugs.swift.org/browse/SR-3104

···

Am 01.11.2016 um 18:08 schrieb Toni Suter via swift-evolution <swift-evolution@swift.org>:

Oh, I didn't see that, thanks! I'll write a bug report.

Am 01.11.2016 um 17:52 schrieb Alex Martini <amartini@apple.com>:

On Nov 1, 2016, at 7:23 AM, Toni Suter via swift-evolution <swift-evolution@swift.org> wrote:

In Swift 3, I can declare a postfix operator that starts with a ? or a !, but as far as I can tell, there's no way to actually use such an operator:

I think it would probably be best to disallow postfix operators that start with a question mark or an exclamation mark.
At the moment it is possible to declare them, but not to use them. That's not very user-friendly.

This looks like a bug — the documentation and the behavior disagree. The docs say:

Although you can define custom operators that contain a question mark (?), they can’t consist of a single question mark character only. Additionally, although operators can contain an exclamation mark (!), postfix operators cannot begin with either a question mark or an exclamation mark.

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/doc/uid/TP40014097-CH30-ID418

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution