Console error

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>

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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-dev

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@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@swift.org <mailto:swift-dev@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@swift.org <mailto:swift-dev@swift.org>
https://lists.swift.org/mailman/listinfo/swift-dev

_______________________________________________
swift-dev mailing list
swift-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-dev

Swift 2.2 produces a better error message for this:

error: '=' must have consistent whitespace on both sides
var i =3
      ^
        
-Chris

···

On Jan 9, 2016, at 2:21 AM, Greg Parker via swift-dev <swift-dev@swift.org> wrote:

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.)