[swift-evolution] define backslash '\' as a operator-head in the swift grammar

David Waite david at alkaline-solutions.com
Tue Feb 7 11:26:18 CST 2017


> On Feb 7, 2017, at 7:23 AM, Derrick Ho via swift-evolution <swift-evolution at swift.org> wrote:
> 
> I have found it strange that operator characters are the only ones that may be used as operators. Sometimes Characters that look like operators are not.
> 
> Why does there need to be a divide?

My understanding -
1. lets the lexer determine if a value is meant to be an identifier or an operator
2. prevents code declaring operators which impact the identifier space
3.-allows identifiers, literals and operators to interact without whitespace. 

For the first point, syntactically ‘cas and ra’ would not be understandable until the compiler has determined that ‘and’ can be an operator, by parsing the rest of the code, which may also not make syntactic sense until the parser has understood other operator declarations 

For the second part, declaring ‘and’ as an operator in a dependent module could impact your app if you had a variable or method named ‘and’.

For the last point - if any character could be an operator, then “casandra" could either be an identifier, or two identifiers (‘cas’ and ‘ra’) being operated on by the ‘and' operator.

So we have a group of name starter characters and operator starter characters to tell when the lever is ending an identifier and beginning an operator or vice-versa, and the two groups have to be non-overlapping.

-DW

> 
> Why can't an arbitrary set of characters become operator-ized?
> 
> For example some people would like "and" to become a binary operator and use it instead of "&&"
> 
> If we had the ability to define an operator out of any set of characters I think it would make the swift language a lot better.
> 
> Is there a technical reason why?
> 



More information about the swift-evolution mailing list