# A new way to allow text and unicode operators?

(Jon Hull) #1

Two impossible features brought together as one possible one...

In the past, we have had many requests for text operators, that is something like:

a and b
x union y


These are not possible because of a technicality of how the compiler's parser works combined with swift's operator syntax (i.e. Is aandb a word itself or a and b without spaces?).

We have also had requests for unicode operators (which are possible... depending on the particular character). But any requests to use them in the standard library have failed due to the difficulty of typing unicode quickly. (There was also the issue of reading the code in editors that don't support unicode)

I was working on something to do with LaTeX, and it gave me an idea. LaTeX can print pretty much any math symbol, and you write the math symbol \symbolName (e.g. \pi).

What if we used a similar approach allowing any word surrounded in single quotes to be declared as an operator using the normal syntax:

infix operator myOp: precedence


In addition, we could associate one of these declarations with a unicode character that represents it. For example (strawman syntax):

infix operator union "\u{222A}": precedence


In use, the operator would still show as text:

let a = x union y


but fancy editors like Xcode would have the option to display the associated unicode character (likely with some visual effect to show it is different than a user typed character. i.e. similar to how color/image literals are handled). Rollover would show the underlying text to aid in learning.

let a = x ∪ y //rolling over shows 'union'


This would make Swift's unicode support much easier to actually use in practice (remembering a word to type is much easier than a number code). It is also backwards compatible with those using editors that only support ascii (they would just see x 'union' y). Graceful degradation...

In the future, single quoted words which haven't been defined as operators could potentially be similarly associated with a unicode display string, and used elsewhere.

let 'dogcow' = "Moof!"


Even if these types of declarations never get used in the standard library, it still makes it much easier for all of us to actually use unicode symbols when we want to...