[swift-users] Request advice on closed range operator

Mark Glossop lists at cueballcentral.com
Sun Feb 14 13:42:06 CST 2016


On 15 Feb 2016, at 01:37, Peter van der Linden via swift-users <swift-users at swift.org> wrote:
> 
> Hi,
> I'm a swift novice, possibly asking an elementary question.
> I noticed that in some cases, a closed range produces a compiler error message.
> 
> If you add whitespace before and after the closed range operator (in the example below), the error message goes away.
> If you delete whitespace before and after the closed range operator, the error message also goes away.
> 
> If you have a space before the closed range operator, but not one after, you get an error message.
> If you have a space after the closed range operator, but not one before, you get an error message.
> Below is a small example that demonstrates the error.  Can anyone explain more about this?  I find it unusual that adding white space between two tokens can cause the compiler to emit an error message.   Should I file a bug about this?
> 
> Thanks
>  Peter
> 
> pvdl$ swift
> Welcome to Apple Swift version 2.1.1 (swiftlang-700.1.101.15 clang-700.1.81). Type :help for assistance.
>  1>         let s1 = "foo"
>  2.         let i = s1.startIndex
>  3.         let j = s1.startIndex.advancedBy(1)
> s1: String = "foo"
> i: Index = {
>  _base = {
>    _position = 0
>    _core = {
>      _baseAddress = 0x000000010054d680
>      _countAndFlags = 3
>      _owner = nil
>    }
>  }
>  _lengthUTF16 = 1
> }
> j: Index = {
>  _base = {
>    _position = 1
>    _core = {
>      _baseAddress = 0x000000010054d680
>      _countAndFlags = 3
>      _owner = nil
>    }
>  }
>  _lengthUTF16 = 1
> }
>  4> var s2 = s1[ i ...j ]
> repl.swift:4:16: error: expected ',' separator
> var s2 = s1[ i ...j ]
>               ^
>              ,
> 
>  4>         var s2 = s1[ i...j ]
> s2: String = "fo"
>  5>         var s3 = s1[ i... j ]
> repl.swift:5:27: error: expected ',' separator
>        var s3 = s1[ i... j ]
>                          ^

This is covered in TSPL in the section on "Operators":
> The whitespace around an operator is used to determine whether an operator is used as a prefix operator, a postfix operator, or a binary operator. This behavior is summarized in the following rules:
> * If an operator has whitespace around both sides or around neither side, it is treated as a binary operator. As an example, the + operator in a+b and a + b is treated as a binary operator.
> * If an operator has whitespace on the left side only, it is treated as a prefix unary operator. As an example, the ++ operator in a ++b is treated as a prefix unary operator.
> * If an operator has whitespace on the right side only, it is treated as a postfix unary operator. As an example, the ++ operator in a++ b is treated as a postfix unary operator.
> * If an operator has no whitespace on the left but is followed immediately by a dot (.), it is treated as a postfix unary operator. As an example, the ++ operator in a++.b is treated as a postfix unary operator (a++ .b rather than a ++ .b).
> 
> Excerpt From: Apple Inc. “The Swift Programming Language (Swift 2.2).” iBooks. 

(I'm assuming the above excerpt isn't significantly different from the Swift 2.1 language, but I didn't check...)

As Jens also indicated, Swift is more particular about whitespace than other C derivates. In the case you've encountered (the first bullet above), you need to have whitespace around both sides or neither side of the range operator; asymmetric whitespace will cause the closed range operator to be parsed as an invalid prefix or postfix operator.

That said, it's probable the error message you've encountered could be a little more informative...

HTH,
M.
-- 
Mark Glossop
E: lists at cueballcentral.com
TW: http://twitter.com/Cueball_AU


More information about the swift-users mailing list