Why is the Swift compiler marking this as an error?










6















I have two ways of writing the same code, one of which seems to be disliked by the Swift compiler. Can you please explain why?



Context:



let guaranteedValue: String
let cursorPositionFromEnd: Int


Working code:



let stringFromEndUntilCursorPosition = String(guaranteedValue.reversed()[0..<cursorPositionFromEnd])


Non-working code:



let reversedOriginalString = guaranteedValue.reversed()
let stringFromEndUntilCursorPosition = String(reversedOriginalString[0..<cursorPositionFromEnd])


Compiler error message: "Cannot subscript a value of type ReversedCollection<String> with an index of type Range<Int>"



Other working attempt:



let reversedOriginalString = guaranteedValue.reversed()[0..< cursorPositionFromEnd]
let stringFromEndUntilCursorPosition = String(reversedOriginalString)


Basically the idea is you can only subscript a reversed range but probably not only if you add the index at a function return, but that does not work if you first reference the variable with a let or var and then try subscripting it.



I also understand that it would probably work in the "non-working code" if the Range would be String.Index type or whatever is the new fashion of doing it.



Can anyone explain why? Is this a bug in Swift's compiler which already has enough "twisted" string logic?










share|improve this question



















  • 2





    Interesting observation... To make things even 'stranger' if you do let x = y.reversed and then let z = x()[0..<cursorPositionFromEnd] doesn't work either... I would love to know why as well...

    – Alladinian
    Nov 15 '18 at 14:11







  • 2





    Compare stackoverflow.com/q/42688679: There are several reversed() methods. In your first example an array is returned, in the second example a “ReversedCollection” is returned.

    – Martin R
    Nov 15 '18 at 14:39















6















I have two ways of writing the same code, one of which seems to be disliked by the Swift compiler. Can you please explain why?



Context:



let guaranteedValue: String
let cursorPositionFromEnd: Int


Working code:



let stringFromEndUntilCursorPosition = String(guaranteedValue.reversed()[0..<cursorPositionFromEnd])


Non-working code:



let reversedOriginalString = guaranteedValue.reversed()
let stringFromEndUntilCursorPosition = String(reversedOriginalString[0..<cursorPositionFromEnd])


Compiler error message: "Cannot subscript a value of type ReversedCollection<String> with an index of type Range<Int>"



Other working attempt:



let reversedOriginalString = guaranteedValue.reversed()[0..< cursorPositionFromEnd]
let stringFromEndUntilCursorPosition = String(reversedOriginalString)


Basically the idea is you can only subscript a reversed range but probably not only if you add the index at a function return, but that does not work if you first reference the variable with a let or var and then try subscripting it.



I also understand that it would probably work in the "non-working code" if the Range would be String.Index type or whatever is the new fashion of doing it.



Can anyone explain why? Is this a bug in Swift's compiler which already has enough "twisted" string logic?










share|improve this question



















  • 2





    Interesting observation... To make things even 'stranger' if you do let x = y.reversed and then let z = x()[0..<cursorPositionFromEnd] doesn't work either... I would love to know why as well...

    – Alladinian
    Nov 15 '18 at 14:11







  • 2





    Compare stackoverflow.com/q/42688679: There are several reversed() methods. In your first example an array is returned, in the second example a “ReversedCollection” is returned.

    – Martin R
    Nov 15 '18 at 14:39













6












6








6


1






I have two ways of writing the same code, one of which seems to be disliked by the Swift compiler. Can you please explain why?



Context:



let guaranteedValue: String
let cursorPositionFromEnd: Int


Working code:



let stringFromEndUntilCursorPosition = String(guaranteedValue.reversed()[0..<cursorPositionFromEnd])


Non-working code:



let reversedOriginalString = guaranteedValue.reversed()
let stringFromEndUntilCursorPosition = String(reversedOriginalString[0..<cursorPositionFromEnd])


Compiler error message: "Cannot subscript a value of type ReversedCollection<String> with an index of type Range<Int>"



Other working attempt:



let reversedOriginalString = guaranteedValue.reversed()[0..< cursorPositionFromEnd]
let stringFromEndUntilCursorPosition = String(reversedOriginalString)


Basically the idea is you can only subscript a reversed range but probably not only if you add the index at a function return, but that does not work if you first reference the variable with a let or var and then try subscripting it.



I also understand that it would probably work in the "non-working code" if the Range would be String.Index type or whatever is the new fashion of doing it.



Can anyone explain why? Is this a bug in Swift's compiler which already has enough "twisted" string logic?










share|improve this question
















I have two ways of writing the same code, one of which seems to be disliked by the Swift compiler. Can you please explain why?



Context:



let guaranteedValue: String
let cursorPositionFromEnd: Int


Working code:



let stringFromEndUntilCursorPosition = String(guaranteedValue.reversed()[0..<cursorPositionFromEnd])


Non-working code:



let reversedOriginalString = guaranteedValue.reversed()
let stringFromEndUntilCursorPosition = String(reversedOriginalString[0..<cursorPositionFromEnd])


Compiler error message: "Cannot subscript a value of type ReversedCollection<String> with an index of type Range<Int>"



Other working attempt:



let reversedOriginalString = guaranteedValue.reversed()[0..< cursorPositionFromEnd]
let stringFromEndUntilCursorPosition = String(reversedOriginalString)


Basically the idea is you can only subscript a reversed range but probably not only if you add the index at a function return, but that does not work if you first reference the variable with a let or var and then try subscripting it.



I also understand that it would probably work in the "non-working code" if the Range would be String.Index type or whatever is the new fashion of doing it.



Can anyone explain why? Is this a bug in Swift's compiler which already has enough "twisted" string logic?







swift string range subscript swift4.2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 18:48









Boann

37.3k1290121




37.3k1290121










asked Nov 15 '18 at 14:01









FawkesFawkes

2,71631633




2,71631633







  • 2





    Interesting observation... To make things even 'stranger' if you do let x = y.reversed and then let z = x()[0..<cursorPositionFromEnd] doesn't work either... I would love to know why as well...

    – Alladinian
    Nov 15 '18 at 14:11







  • 2





    Compare stackoverflow.com/q/42688679: There are several reversed() methods. In your first example an array is returned, in the second example a “ReversedCollection” is returned.

    – Martin R
    Nov 15 '18 at 14:39












  • 2





    Interesting observation... To make things even 'stranger' if you do let x = y.reversed and then let z = x()[0..<cursorPositionFromEnd] doesn't work either... I would love to know why as well...

    – Alladinian
    Nov 15 '18 at 14:11







  • 2





    Compare stackoverflow.com/q/42688679: There are several reversed() methods. In your first example an array is returned, in the second example a “ReversedCollection” is returned.

    – Martin R
    Nov 15 '18 at 14:39







2




2





Interesting observation... To make things even 'stranger' if you do let x = y.reversed and then let z = x()[0..<cursorPositionFromEnd] doesn't work either... I would love to know why as well...

– Alladinian
Nov 15 '18 at 14:11






Interesting observation... To make things even 'stranger' if you do let x = y.reversed and then let z = x()[0..<cursorPositionFromEnd] doesn't work either... I would love to know why as well...

– Alladinian
Nov 15 '18 at 14:11





2




2





Compare stackoverflow.com/q/42688679: There are several reversed() methods. In your first example an array is returned, in the second example a “ReversedCollection” is returned.

– Martin R
Nov 15 '18 at 14:39





Compare stackoverflow.com/q/42688679: There are several reversed() methods. In your first example an array is returned, in the second example a “ReversedCollection” is returned.

– Martin R
Nov 15 '18 at 14:39












1 Answer
1






active

oldest

votes


















5














There are several reversed() methods, as explained in What trouble could bring assining reversed() to a swift array?.



In your first code example,



let stringFromEndUntilCursorPosition = String(guaranteedValue.reversed()[0..<cursorPositionFromEnd])


the compiler infers from the context (i.e. the subscript) that the
Sequence.reversed() method is needed, which returns an array. Arrays are indexed by integers,
therefore the code compiles. This method has O(n) complexity because a
new array with all elements is created.



In



let reversedOriginalString = guaranteedValue.reversed()


there is no such context, and the compiler chooses the
Bidirectional.reversed() method. Compared to the above mentioned method, this one has
O(1) complexity. It returns a ReversedCollection which
has its own index type, therefore



let stringFromEndUntilCursorPosition = String(reversedOriginalString[0..<cursorPositionFromEnd])


produces the observed error message.



Possible solutions:




  • Provide context to enforce the array creation:



    let reversedOriginalString: [Character] = guaranteedValue.reversed()
    // Or: let reversedOriginalString: Array = guaranteedValue.reversed()
    // Or: let reversedOriginalString = guaranteedValue.reversed() as Array
    let stringFromEndUntilCursorPosition = String(reversedOriginalString[0..<cursorPositionFromEnd])


    This works, but has the disadvantage of creating a temporary array.




  • Do the proper index calculations on the reversed collection:



    let reversedOriginalString = guaranteedValue.reversed()
    let pos = reversedOriginalString.index(reversedOriginalString.startIndex, offsetBy: cursorPositionFromEnd)
    let stringFromEndUntilCursorPosition = String(reversedOriginalString[..<pos])


    This avoids the intermediate array, but is tedious to write.




  • Use the prefix(maxLength:) method of sequences:



    let reversedOriginalString = guaranteedValue.reversed()
    let stringFromEndUntilCursorPosition = String(reversedOriginalString.prefix(cursorPositionFromEnd))






share|improve this answer

























  • "the compiler chooses" sounds like a bug as the compiler is capable of reporting ambiguity in others cases, or am I missing something and its just a documented feature?

    – CRD
    Nov 15 '18 at 16:17











  • @CRD: As I understand it, the compiler chooses the most specific method, considering both the arguments and the context (the required return type). String is a BirectionalCollection (which is more specific than Sequence, but is not a RandomAccessCollection). Therefore BirectionalCollection.reversed() is chosen without more context. If the compiler sees that an Array should be returned, Sequence.reversed() is chosen. HTH :)

    – Martin R
    Nov 15 '18 at 18:31











  • Choosing the "most specific" is the right thing to do if it is well-defined, and that's what I went looking for after I read your answer but failed to find a specification - which doesn't mean there isn't one I missed of course! Without a well-defined spec...

    – CRD
    Nov 15 '18 at 18:50











  • Further searching suggests that most specific might be "defined" in the compiler source (see this question). C++ source files are not the place to define language semantics :-(, especially when they contain comments like "this is a hack" :-( :-( One would hope that Apple knew better than this by now, maybe it is defined elsewhere...

    – CRD
    Nov 15 '18 at 22:19











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53321158%2fwhy-is-the-swift-compiler-marking-this-as-an-error%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









5














There are several reversed() methods, as explained in What trouble could bring assining reversed() to a swift array?.



In your first code example,



let stringFromEndUntilCursorPosition = String(guaranteedValue.reversed()[0..<cursorPositionFromEnd])


the compiler infers from the context (i.e. the subscript) that the
Sequence.reversed() method is needed, which returns an array. Arrays are indexed by integers,
therefore the code compiles. This method has O(n) complexity because a
new array with all elements is created.



In



let reversedOriginalString = guaranteedValue.reversed()


there is no such context, and the compiler chooses the
Bidirectional.reversed() method. Compared to the above mentioned method, this one has
O(1) complexity. It returns a ReversedCollection which
has its own index type, therefore



let stringFromEndUntilCursorPosition = String(reversedOriginalString[0..<cursorPositionFromEnd])


produces the observed error message.



Possible solutions:




  • Provide context to enforce the array creation:



    let reversedOriginalString: [Character] = guaranteedValue.reversed()
    // Or: let reversedOriginalString: Array = guaranteedValue.reversed()
    // Or: let reversedOriginalString = guaranteedValue.reversed() as Array
    let stringFromEndUntilCursorPosition = String(reversedOriginalString[0..<cursorPositionFromEnd])


    This works, but has the disadvantage of creating a temporary array.




  • Do the proper index calculations on the reversed collection:



    let reversedOriginalString = guaranteedValue.reversed()
    let pos = reversedOriginalString.index(reversedOriginalString.startIndex, offsetBy: cursorPositionFromEnd)
    let stringFromEndUntilCursorPosition = String(reversedOriginalString[..<pos])


    This avoids the intermediate array, but is tedious to write.




  • Use the prefix(maxLength:) method of sequences:



    let reversedOriginalString = guaranteedValue.reversed()
    let stringFromEndUntilCursorPosition = String(reversedOriginalString.prefix(cursorPositionFromEnd))






share|improve this answer

























  • "the compiler chooses" sounds like a bug as the compiler is capable of reporting ambiguity in others cases, or am I missing something and its just a documented feature?

    – CRD
    Nov 15 '18 at 16:17











  • @CRD: As I understand it, the compiler chooses the most specific method, considering both the arguments and the context (the required return type). String is a BirectionalCollection (which is more specific than Sequence, but is not a RandomAccessCollection). Therefore BirectionalCollection.reversed() is chosen without more context. If the compiler sees that an Array should be returned, Sequence.reversed() is chosen. HTH :)

    – Martin R
    Nov 15 '18 at 18:31











  • Choosing the "most specific" is the right thing to do if it is well-defined, and that's what I went looking for after I read your answer but failed to find a specification - which doesn't mean there isn't one I missed of course! Without a well-defined spec...

    – CRD
    Nov 15 '18 at 18:50











  • Further searching suggests that most specific might be "defined" in the compiler source (see this question). C++ source files are not the place to define language semantics :-(, especially when they contain comments like "this is a hack" :-( :-( One would hope that Apple knew better than this by now, maybe it is defined elsewhere...

    – CRD
    Nov 15 '18 at 22:19
















5














There are several reversed() methods, as explained in What trouble could bring assining reversed() to a swift array?.



In your first code example,



let stringFromEndUntilCursorPosition = String(guaranteedValue.reversed()[0..<cursorPositionFromEnd])


the compiler infers from the context (i.e. the subscript) that the
Sequence.reversed() method is needed, which returns an array. Arrays are indexed by integers,
therefore the code compiles. This method has O(n) complexity because a
new array with all elements is created.



In



let reversedOriginalString = guaranteedValue.reversed()


there is no such context, and the compiler chooses the
Bidirectional.reversed() method. Compared to the above mentioned method, this one has
O(1) complexity. It returns a ReversedCollection which
has its own index type, therefore



let stringFromEndUntilCursorPosition = String(reversedOriginalString[0..<cursorPositionFromEnd])


produces the observed error message.



Possible solutions:




  • Provide context to enforce the array creation:



    let reversedOriginalString: [Character] = guaranteedValue.reversed()
    // Or: let reversedOriginalString: Array = guaranteedValue.reversed()
    // Or: let reversedOriginalString = guaranteedValue.reversed() as Array
    let stringFromEndUntilCursorPosition = String(reversedOriginalString[0..<cursorPositionFromEnd])


    This works, but has the disadvantage of creating a temporary array.




  • Do the proper index calculations on the reversed collection:



    let reversedOriginalString = guaranteedValue.reversed()
    let pos = reversedOriginalString.index(reversedOriginalString.startIndex, offsetBy: cursorPositionFromEnd)
    let stringFromEndUntilCursorPosition = String(reversedOriginalString[..<pos])


    This avoids the intermediate array, but is tedious to write.




  • Use the prefix(maxLength:) method of sequences:



    let reversedOriginalString = guaranteedValue.reversed()
    let stringFromEndUntilCursorPosition = String(reversedOriginalString.prefix(cursorPositionFromEnd))






share|improve this answer

























  • "the compiler chooses" sounds like a bug as the compiler is capable of reporting ambiguity in others cases, or am I missing something and its just a documented feature?

    – CRD
    Nov 15 '18 at 16:17











  • @CRD: As I understand it, the compiler chooses the most specific method, considering both the arguments and the context (the required return type). String is a BirectionalCollection (which is more specific than Sequence, but is not a RandomAccessCollection). Therefore BirectionalCollection.reversed() is chosen without more context. If the compiler sees that an Array should be returned, Sequence.reversed() is chosen. HTH :)

    – Martin R
    Nov 15 '18 at 18:31











  • Choosing the "most specific" is the right thing to do if it is well-defined, and that's what I went looking for after I read your answer but failed to find a specification - which doesn't mean there isn't one I missed of course! Without a well-defined spec...

    – CRD
    Nov 15 '18 at 18:50











  • Further searching suggests that most specific might be "defined" in the compiler source (see this question). C++ source files are not the place to define language semantics :-(, especially when they contain comments like "this is a hack" :-( :-( One would hope that Apple knew better than this by now, maybe it is defined elsewhere...

    – CRD
    Nov 15 '18 at 22:19














5












5








5







There are several reversed() methods, as explained in What trouble could bring assining reversed() to a swift array?.



In your first code example,



let stringFromEndUntilCursorPosition = String(guaranteedValue.reversed()[0..<cursorPositionFromEnd])


the compiler infers from the context (i.e. the subscript) that the
Sequence.reversed() method is needed, which returns an array. Arrays are indexed by integers,
therefore the code compiles. This method has O(n) complexity because a
new array with all elements is created.



In



let reversedOriginalString = guaranteedValue.reversed()


there is no such context, and the compiler chooses the
Bidirectional.reversed() method. Compared to the above mentioned method, this one has
O(1) complexity. It returns a ReversedCollection which
has its own index type, therefore



let stringFromEndUntilCursorPosition = String(reversedOriginalString[0..<cursorPositionFromEnd])


produces the observed error message.



Possible solutions:




  • Provide context to enforce the array creation:



    let reversedOriginalString: [Character] = guaranteedValue.reversed()
    // Or: let reversedOriginalString: Array = guaranteedValue.reversed()
    // Or: let reversedOriginalString = guaranteedValue.reversed() as Array
    let stringFromEndUntilCursorPosition = String(reversedOriginalString[0..<cursorPositionFromEnd])


    This works, but has the disadvantage of creating a temporary array.




  • Do the proper index calculations on the reversed collection:



    let reversedOriginalString = guaranteedValue.reversed()
    let pos = reversedOriginalString.index(reversedOriginalString.startIndex, offsetBy: cursorPositionFromEnd)
    let stringFromEndUntilCursorPosition = String(reversedOriginalString[..<pos])


    This avoids the intermediate array, but is tedious to write.




  • Use the prefix(maxLength:) method of sequences:



    let reversedOriginalString = guaranteedValue.reversed()
    let stringFromEndUntilCursorPosition = String(reversedOriginalString.prefix(cursorPositionFromEnd))






share|improve this answer















There are several reversed() methods, as explained in What trouble could bring assining reversed() to a swift array?.



In your first code example,



let stringFromEndUntilCursorPosition = String(guaranteedValue.reversed()[0..<cursorPositionFromEnd])


the compiler infers from the context (i.e. the subscript) that the
Sequence.reversed() method is needed, which returns an array. Arrays are indexed by integers,
therefore the code compiles. This method has O(n) complexity because a
new array with all elements is created.



In



let reversedOriginalString = guaranteedValue.reversed()


there is no such context, and the compiler chooses the
Bidirectional.reversed() method. Compared to the above mentioned method, this one has
O(1) complexity. It returns a ReversedCollection which
has its own index type, therefore



let stringFromEndUntilCursorPosition = String(reversedOriginalString[0..<cursorPositionFromEnd])


produces the observed error message.



Possible solutions:




  • Provide context to enforce the array creation:



    let reversedOriginalString: [Character] = guaranteedValue.reversed()
    // Or: let reversedOriginalString: Array = guaranteedValue.reversed()
    // Or: let reversedOriginalString = guaranteedValue.reversed() as Array
    let stringFromEndUntilCursorPosition = String(reversedOriginalString[0..<cursorPositionFromEnd])


    This works, but has the disadvantage of creating a temporary array.




  • Do the proper index calculations on the reversed collection:



    let reversedOriginalString = guaranteedValue.reversed()
    let pos = reversedOriginalString.index(reversedOriginalString.startIndex, offsetBy: cursorPositionFromEnd)
    let stringFromEndUntilCursorPosition = String(reversedOriginalString[..<pos])


    This avoids the intermediate array, but is tedious to write.




  • Use the prefix(maxLength:) method of sequences:



    let reversedOriginalString = guaranteedValue.reversed()
    let stringFromEndUntilCursorPosition = String(reversedOriginalString.prefix(cursorPositionFromEnd))







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 15:11

























answered Nov 15 '18 at 14:57









Martin RMartin R

402k56889989




402k56889989












  • "the compiler chooses" sounds like a bug as the compiler is capable of reporting ambiguity in others cases, or am I missing something and its just a documented feature?

    – CRD
    Nov 15 '18 at 16:17











  • @CRD: As I understand it, the compiler chooses the most specific method, considering both the arguments and the context (the required return type). String is a BirectionalCollection (which is more specific than Sequence, but is not a RandomAccessCollection). Therefore BirectionalCollection.reversed() is chosen without more context. If the compiler sees that an Array should be returned, Sequence.reversed() is chosen. HTH :)

    – Martin R
    Nov 15 '18 at 18:31











  • Choosing the "most specific" is the right thing to do if it is well-defined, and that's what I went looking for after I read your answer but failed to find a specification - which doesn't mean there isn't one I missed of course! Without a well-defined spec...

    – CRD
    Nov 15 '18 at 18:50











  • Further searching suggests that most specific might be "defined" in the compiler source (see this question). C++ source files are not the place to define language semantics :-(, especially when they contain comments like "this is a hack" :-( :-( One would hope that Apple knew better than this by now, maybe it is defined elsewhere...

    – CRD
    Nov 15 '18 at 22:19


















  • "the compiler chooses" sounds like a bug as the compiler is capable of reporting ambiguity in others cases, or am I missing something and its just a documented feature?

    – CRD
    Nov 15 '18 at 16:17











  • @CRD: As I understand it, the compiler chooses the most specific method, considering both the arguments and the context (the required return type). String is a BirectionalCollection (which is more specific than Sequence, but is not a RandomAccessCollection). Therefore BirectionalCollection.reversed() is chosen without more context. If the compiler sees that an Array should be returned, Sequence.reversed() is chosen. HTH :)

    – Martin R
    Nov 15 '18 at 18:31











  • Choosing the "most specific" is the right thing to do if it is well-defined, and that's what I went looking for after I read your answer but failed to find a specification - which doesn't mean there isn't one I missed of course! Without a well-defined spec...

    – CRD
    Nov 15 '18 at 18:50











  • Further searching suggests that most specific might be "defined" in the compiler source (see this question). C++ source files are not the place to define language semantics :-(, especially when they contain comments like "this is a hack" :-( :-( One would hope that Apple knew better than this by now, maybe it is defined elsewhere...

    – CRD
    Nov 15 '18 at 22:19

















"the compiler chooses" sounds like a bug as the compiler is capable of reporting ambiguity in others cases, or am I missing something and its just a documented feature?

– CRD
Nov 15 '18 at 16:17





"the compiler chooses" sounds like a bug as the compiler is capable of reporting ambiguity in others cases, or am I missing something and its just a documented feature?

– CRD
Nov 15 '18 at 16:17













@CRD: As I understand it, the compiler chooses the most specific method, considering both the arguments and the context (the required return type). String is a BirectionalCollection (which is more specific than Sequence, but is not a RandomAccessCollection). Therefore BirectionalCollection.reversed() is chosen without more context. If the compiler sees that an Array should be returned, Sequence.reversed() is chosen. HTH :)

– Martin R
Nov 15 '18 at 18:31





@CRD: As I understand it, the compiler chooses the most specific method, considering both the arguments and the context (the required return type). String is a BirectionalCollection (which is more specific than Sequence, but is not a RandomAccessCollection). Therefore BirectionalCollection.reversed() is chosen without more context. If the compiler sees that an Array should be returned, Sequence.reversed() is chosen. HTH :)

– Martin R
Nov 15 '18 at 18:31













Choosing the "most specific" is the right thing to do if it is well-defined, and that's what I went looking for after I read your answer but failed to find a specification - which doesn't mean there isn't one I missed of course! Without a well-defined spec...

– CRD
Nov 15 '18 at 18:50





Choosing the "most specific" is the right thing to do if it is well-defined, and that's what I went looking for after I read your answer but failed to find a specification - which doesn't mean there isn't one I missed of course! Without a well-defined spec...

– CRD
Nov 15 '18 at 18:50













Further searching suggests that most specific might be "defined" in the compiler source (see this question). C++ source files are not the place to define language semantics :-(, especially when they contain comments like "this is a hack" :-( :-( One would hope that Apple knew better than this by now, maybe it is defined elsewhere...

– CRD
Nov 15 '18 at 22:19






Further searching suggests that most specific might be "defined" in the compiler source (see this question). C++ source files are not the place to define language semantics :-(, especially when they contain comments like "this is a hack" :-( :-( One would hope that Apple knew better than this by now, maybe it is defined elsewhere...

– CRD
Nov 15 '18 at 22:19




















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53321158%2fwhy-is-the-swift-compiler-marking-this-as-an-error%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto