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

Nicolas Fezans nicolas.fezans at gmail.com
Sun Feb 5 10:19:42 CST 2017


Well you cannot define it because the \ character itself is not allowed
outside of a string.
If you try to declare it with

infix operator \


you get an error "Invalid character in source file" in Xcode and where you
define the function itself you have the same error.


This would not happen if it was part of the allowed operator-head
characters. Here is the grammar of operator for Swift 3.0.1 (from
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/zzSummaryOfTheGrammar.html
)


GRAMMAR OF OPERATORS
operator → operator-head­operator-characters­opt­
operator → dot-operator-head­dot-operator-characters­

operator-head → /­  =­  -­  +­  !­  *­  %­  <­  >­  &­  |­  ^­  ~­  ?­
operator-head → U+00A1–U+00A7
operator-head → U+00A9 or U+00AB
operator-head → U+00AC or U+00AE
operator-head → U+00B0–U+00B1, U+00B6, U+00BB, U+00BF, U+00D7, or U+00F7
operator-head → U+2016–U+2017 or U+2020–U+2027
operator-head → U+2030–U+203E
operator-head → U+2041–U+2053
operator-head → U+2055–U+205E
operator-head → U+2190–U+23FF
operator-head → U+2500–U+2775
operator-head → U+2794–U+2BFF
operator-head → U+2E00–U+2E7F
operator-head → U+3001–U+3003
operator-head → U+3008–U+3030

operator-character → operator-head­
operator-character → U+0300–U+036F
operator-character → U+1DC0–U+1DFF
operator-character → U+20D0–U+20FF
operator-character → U+FE00–U+FE0F
operator-character → U+FE20–U+FE2F
operator-character → U+E0100–U+E01EF
operator-characters → operator-character­operator-characters­opt­

dot-operator-head → .­
dot-operator-character → .­  operator-character­
dot-operator-characters →
dot-operator-character­dot-operator-characters­opt­

binary-operator → operator­
prefix-operator → operator­
postfix-operator → operator­


The change I propose would basically lead to have
operator-head → /­  =­  -­  +­  !­  *­  %­  <­  >­  &­  |­  ^­  ~­  ?­ \

instead of the line

operator-head → /­  =­  -­  +­  !­  *­  %­  <­  >­  &­  |­  ^­  ~­  ?­

in the swift grammar.


Nicolas




 wi

On Sun, Feb 5, 2017 at 4:55 PM, Derrick Ho <wh1pch81n at gmail.com> wrote:

> If the \ operator is not defined in swift, does it treat it as something
> that can be operator overloaded?
>
>
> On Sun, Feb 5, 2017 at 10:29 AM Nicolas Fezans via swift-evolution <
> swift-evolution at swift.org> wrote:
>
>> Dear all,
>>
>> This is a rather simple proposal to add '\' (backslash character) as a
>> valid operator-head in the swift grammar.
>>
>> One argument for it, is that there exist a backslash operator in the
>> MATLAB/Scilab/Octave languages. In this languages A\B solves the linear
>> system A*X = B for X (or the least square problem associated to it if the
>> system of equations is overdetermined). I am doing some numerical
>> computation in Swift and it would be nice to be able to declare the same
>> operator name for this functionality.
>>
>> I might have missed some arguments for not adding them, but I seems to me
>> that until now the \ character is only used inside of string literals. If
>> that is the case, both uses should never generate a conflict or be
>> ambiguous, isn't it? (String literals keep their interpretation of \ and \
>> used otherwise within the swift code will be interpreted as an operator or
>> as the beginning of an operator)
>>
>> I am curious to see what will be the feedback on this.
>>
>> Nicolas
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170205/61b601b5/attachment.html>


More information about the swift-evolution mailing list