' Operator - cannot be defined

As I interpret the Swift documentation, one should be able to use the prime ( ' ) character among other quote combinations as a custom operator.


Specifically, I'd like to use the back-tic ( ` ) and prime ( ' ) as prefix and/or postfix operators.


This code doesn't compile


postfix operator '


postfix func ' <A>(arg: A) -> A {

return arg

}


23'


whereas this works fine


postfix operator ~


postfix func ~ <A>(arg: A) -> A {

return arg

}


23~


Is this simply not possible?

Accepted Answer

As I interpret the Swift documentation, one should be able to use the prime ( ' ) character among other quote combinations as a custom operator.


You may be misterpreting the Swift doc, the characters you have shown ' (U+0027 APOSTROPHE) or ` (U+0060 GRAVE ACCENT) are not included in the operator-head characters.


You need to choose other characters listed in the operator-head characters.

For future reference

Swift Operators - A Visual Reference

http://applifebalance.com/posts/swiftopsbeta4/

FYI, there is a move afoot to use U+0027 to denote

Character
literals. It’s not quite an official Swift Evolution proposal yet, but it’s getting close. See this thread over on Swift Forums.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
' Operator - cannot be defined
 
 
Q