[swift-dev] console error

Greg Parker gparker at apple.com
Sat Jan 9 04:21:44 CST 2016


That's right. Swift's operators are whitespace-sensitive. The rules are:
* Whitespace on both sides: infix operator
* Whitespace on neither side: infix operator
* Whitespace on only one side: prefix or postfix operator

Therefore the expression `i =3` uses prefix operator = . There is no such operator, and it's reserved so you can't write your own.
`i = 3` and `i=3` and `i =     3` are both the ordinary infix operator = .

Obligatory open-source opportunity: add a compiler fix-it for this. (Perhaps just the prefix/postfix = case specifically, perhaps the more general case where the requested operator does not exist but an operator with different fixity does.)


> On Jan 8, 2016, at 10:54 PM, Jo Albright via swift-dev <swift-dev at swift.org> wrote:
> 
> Hi Zhaoliang,
> 
> Try adding a space between the “=“ & “3” 
> 
> var i = 3
> 
> instead of
> 
> var i =3
> 
> That should fix the issue.
> 
> The second way makes it think you are trying to use it as a prefix operator instead of an assignment.
> 
>  Nerd . Designer . Developer
> Jo Albright
> 
> 
>> On Jan 9, 2016, at 12:07 AM, zhaoliang via swift-dev <swift-dev at swift.org <mailto:swift-dev at swift.org>> wrote:
>> 
>> hello ,
>>     I find a bug like this:
>> 
>>     zhaoliangdeMacBook-Pro:~ zhaoliang$ swift
>> Welcome to Apple Swift version 2.1.1 (swiftlang-700.1.101.15 clang-700.1.81). Type :help for assistance.
>>   1> var i =3
>> repl.swift:1:7: error: prefix '=' is reserved
>> var i =3
>>       ^
>> 
>>   1> var i=3
>> i: Int = 3
>>   2>  
>> 
>>  
>> 
>> _______________________________________________
>> swift-dev mailing list
>> swift-dev at swift.org <mailto:swift-dev at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-dev
> 
> 
> _______________________________________________
> swift-dev mailing list
> swift-dev at swift.org
> https://lists.swift.org/mailman/listinfo/swift-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-dev/attachments/20160109/971cdb2b/attachment.html>


More information about the swift-dev mailing list