Array's (Collection's?) `.first` property already returns element 0 as an
optional, so your first use case is already covered.
For the second point, I'll back this up with a less hacky example: accessing
the detail viewcontroller, if present, from a SplitViewController loaded from a
Responses inline
>
> On May 7, 2017 at 15:16, mailto:and...@projectdent.com)>
> wrote:
>
>
>
>
>
>
> To respond to one of the main points I’ve seen, which is that this isn’t
> necessary for arrays because the index you’re using is often tied to
To respond to one of the main points I’ve seen, which is that this isn’t
necessary for arrays because the index you’re using is often tied to data
related to the array, such as in the example I gave originally, where in
`cellForRowAtIndexPath`, the index path is probably informed by the length
of
on Mon Apr 17 2017, Karl Wagner wrote:
>> On 17 Apr 2017, at 18:35, Dave Abrahams via swift-evolution
>> wrote:
>>
>>
>> on Fri Apr 14 2017, Karl Wagner wrote:
>>
>
>>> Personally, the only valid use-case I
> Dynamic programming comes to mind.
Wouldn’t you then be able to use Array(repeating:count:) and repeat 0 (or
something else) to achieve this then?
Yes, less performant than alloc’ing array (since we need to fill in default
values), but doing otherwise would go against Swift’s safety model.
> On 17 Apr 2017, at 18:35, Dave Abrahams via swift-evolution
> wrote:
>
>
> on Fri Apr 14 2017, Karl Wagner wrote:
>
>> Personally, the only valid use-case I can think of is when you want to
>> initialise an Array’s elements
on Fri Apr 14 2017, Karl Wagner wrote:
> Personally, the only valid use-case I can think of is when you want to
> initialise an Array’s elements out-of-order - i.e., you want to set a
> value for myArray[2] despite myArray[0] and [1] not being
> populated. In that
Dynamic programming comes to mind.
Saagar Jha
> On Apr 16, 2017, at 19:33, Riley Testut wrote:
>
> My bad, should have phrased my response better :^)
>
> Under what circumstances would you need to be able to assign elements in an
> array out of order, while also
My bad, should have phrased my response better :^)
Under what circumstances would you need to be able to assign elements in an
array out of order, while also requiring Array size/performance? (Genuinely
curious, not trying to attack).
IMO, if the differences between Array and Dictionary would
A Dictionary uses a lot more space than an Array, though, and allow for bogus
keys like “-1”, etc.
Saagar Jha
> On Apr 16, 2017, at 10:34, Riley Testut via swift-evolution
> wrote:
>
>> Personally, the only valid use-case I can think of is when you want to
>>
> Personally, the only valid use-case I can think of is when you want to
> initialise an Array’s elements out-of-order - i.e., you want to set a value
> for myArray[2] despite myArray[0] and [1] not being populated. In that case,
> it would be better to have some kind of SparseArray type, and
on Thu Apr 13 2017, David Sweeris wrote:
>> On Apr 13, 2017, at 3:56 AM, Andrew Hart via swift-evolution
> wrote:
>>
>> Recently I’ve been considering the lack of safety around array indexes.
>> Swift is designed with
> safety in mind, so
> I'd actually say the #1 reason not to add this feature is that a lot of
> developers don't seem to understand this, and they're likely to use the
> feature to make their code try to continue in the face of programmer error
> instead of trapping like it properly should. A program in an
> On Apr 13, 2017, at 5:19 AM, David Sweeris via swift-evolution
> wrote:
>
>>
>> On Apr 13, 2017, at 3:56 AM, Andrew Hart via swift-evolution
>> > wrote:
>>
>> Recently I’ve been considering the lack of
I think the subscript index should not be optional - you want 2nd array
element, 3rd, etc., not a "nil" element - this makes no sense to me. Also it
feels like some strange optional behavior compared to the normal usage, so -1
from me on this idea.
I think that there can be situations where
A proposal for lenient subscripts has already been written, and a PR has
been created on the swift-evolution repo and it has been closed for not
being in scope. When it is in scope, it can be considered again. But in the
meantime, the topic has been visited so many times that it's extremely
> On Apr 13, 2017, at 15:01, Tony Allevato wrote:
>
> My 2 cents: Giving users checked subscripts for arrays will do more harm than
> good for novice programmers—they're going to do the wrong thing with them.
I'm torn... On the one hand, I see the logic in this. On
On Thu, Apr 13, 2017 at 2:55 PM Brent Royal-Gordon via swift-evolution <
swift-evolution@swift.org> wrote:
> On Apr 13, 2017, at 9:18 AM, David Sweeris via swift-evolution <
> swift-evolution@swift.org> wrote:
>
> In any case, given that we're discussing a "safe subscript", we need a way
> to
> On Apr 13, 2017, at 9:18 AM, David Sweeris via swift-evolution
> wrote:
>
> In any case, given that we're discussing a "safe subscript", we need a way to
> differentiate the safe subscript from the unsafe subscript. The only two ways
> to do that are to either add
Yes, the idea was to key off of an optional index. something like this:
> extension Array {
> public subscript(_ index: Int?) -> Element? {
> guard let i = index, i < endIndex else { return nil }
>
> return self[i]
> }
> }
>
> let array = ["foo", "bar", "baz"]
>
> On Apr 13, 2017, at 08:53, Josh Parmenter wrote:
>
> This seems inconsistent to me. 2 is 2... 2 itself is not optional. You
> wouldn't expect 2 to be unwrapped.
Correct. I think the idea was that "2?" would get converted to an
`Optional`, which would then call a
This seems inconsistent to me. 2 is 2... 2 itself is not optional. You wouldn't
expect 2 to be unwrapped.
Best
Josh
Sent from my iPhone
On Apr 13, 2017, at 08:48, Jeff Kelley via swift-evolution
> wrote:
Apologies if this has been
I think most people would agree with “its a Not a Great Idea (™)”.
The root argument is “out-of-bounds array access is a logic error”.
> On 13.04.2017, at 16:54, David Sweeris via swift-evolution
> wrote:
>
>
>
>
> Sent from my iPhone
> On Apr 13, 2017, at 07:34,
Sent from my iPhone
> On Apr 13, 2017, at 07:34, Jeff Kelley wrote:
>
> Apologies if this has been suggested before, but going off of Andrew’s
> message, a simple syntax could be to use our existing Optional syntax:
>
> let array = ["foo", "bar", "baz"]
>
> let first
AFAIK arrays use the same “subscripts” [1] feature as other collections. So
this feature would have to be tagged onto subscripts in general.
I see a few problems with you suggestion:
- How do you differentiate between an out-of-bounds subscript access in an
array with optional elements, e.g.
Apologies if this has been suggested before, but going off of Andrew’s message,
a simple syntax could be to use our existing Optional syntax:
let array = ["foo", "bar", "baz"]
let first = array[0] // type is `String`
let third = array[2?] // type is `String?`, value is .some("baz")
let fourth =
> On Apr 13, 2017, at 3:56 AM, Andrew Hart via swift-evolution
> wrote:
>
> Recently I’ve been considering the lack of safety around array indexes. Swift
> is designed with safety in mind, so this example would not compile:
>
> var myString: String? = “hello”
>
27 matches
Mail list logo