Why is the Swift compiler marking this as an error?
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
add a comment |
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
2
Interesting observation... To make things even 'stranger' if you dolet x = y.reversed
and thenlet 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 severalreversed()
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
add a comment |
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
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
swift string range subscript swift4.2
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 dolet x = y.reversed
and thenlet 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 severalreversed()
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
add a comment |
2
Interesting observation... To make things even 'stranger' if you dolet x = y.reversed
and thenlet 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 severalreversed()
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
add a comment |
1 Answer
1
active
oldest
votes
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 theSequence.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 theBidirectional.reversed()
method. Compared to the above mentioned method, this one hasO(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))
"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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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 theSequence.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 theBidirectional.reversed()
method. Compared to the above mentioned method, this one hasO(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))
"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
add a comment |
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 theSequence.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 theBidirectional.reversed()
method. Compared to the above mentioned method, this one hasO(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))
"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
add a comment |
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 theSequence.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 theBidirectional.reversed()
method. Compared to the above mentioned method, this one hasO(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))
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 theSequence.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 theBidirectional.reversed()
method. Compared to the above mentioned method, this one hasO(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))
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
add a comment |
"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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
2
Interesting observation... To make things even 'stranger' if you do
let x = y.reversed
and thenlet 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