[swift-evolution] [Idea] Custom keywords for operators.

Daryle Walker darylew at mac.com
Mon Jul 31 17:06:32 CDT 2017

> On Jul 31, 2017, at 5:09 PM, Gor Gyolchanyan via swift-evolution <swift-evolution at swift.org> wrote:
> So I was thinking the other day (and by "the other day" I mean "It just occurred to me") that Swift's custom operator declaration mechanism is pretty sweet (it's become even sweeter ever since numeric precedence values were replaced with purely relativistic precedence trees). There are currently only two problems with them that grind my operator-declaring endeavors to a painful halt:
> 	1. The fact that most punctuation characters on the keyboard (think - ASCII) are reserved, so any custom operator either has to be a long sequence of two or three non-reserved ASCII characters or have to include difficult-to-type unicode punctuation characters.
> 	2. The fact that anything that passes as an identifier character (which includes a surprisingly wide array of punctuation characters) is off the table as well.
> I have no good idea how to deal with the first problem, but the second problem seems to have a bafflingly simple solution that I can't believe I haven't noticed until now.
> And the reason to even try to solve that problem is because Swift already has a lot of operators with identifiers in them:
> 	* infix is
> 	* infix as
> 	* infix as?
> 	* infix as!
> 	* prefix try
> 	* prefix try?
> 	* prefix try!
> 	* prefix throw
> So this is hardly a new concept to Schwifty developers.

I think I read on some thread here that the sets of token sequences for identifiers and operators are deliberately distinct, so basic parsing agents can be made. This can’t be done in C++ because some tokens are too context-sensitive for anything less than a full parser; the Swift authors didn’t want this to happen here. The keyword operators you gave aren’t a general rule, but custom syntax. They work because the exception list is limited, and probably won’t be generally open.

Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT mac DOT com 

More information about the swift-evolution mailing list