Re: [swift-evolution] [Pitch] Adding safety to arrays

2017-05-07 Thread James Froggatt via swift-evolution
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

Re: [swift-evolution] [Pitch] Adding safety to arrays

2017-05-07 Thread Karl Wagner via swift-evolution
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

Re: [swift-evolution] [Pitch] Adding safety to arrays

2017-05-07 Thread Andrew Hart via swift-evolution
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

Re: [swift-evolution] [Pitch] Adding safety to arrays

2017-04-17 Thread Dave Abrahams via swift-evolution
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

Re: [swift-evolution] [Pitch] Adding safety to arrays

2017-04-17 Thread Riley Testut via swift-evolution
> 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.

Re: [swift-evolution] [Pitch] Adding safety to arrays

2017-04-17 Thread Karl Wagner via swift-evolution
> 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

Re: [swift-evolution] [Pitch] Adding safety to arrays

2017-04-17 Thread Dave Abrahams via swift-evolution
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

Re: [swift-evolution] [Pitch] Adding safety to arrays

2017-04-16 Thread Saagar Jha via swift-evolution
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

Re: [swift-evolution] [Pitch] Adding safety to arrays

2017-04-16 Thread Riley Testut via swift-evolution
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

Re: [swift-evolution] [Pitch] Adding safety to arrays

2017-04-16 Thread Saagar Jha via swift-evolution
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 >>

Re: [swift-evolution] [Pitch] Adding safety to arrays

2017-04-16 Thread Riley Testut via swift-evolution
> 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

Re: [swift-evolution] [Pitch] Adding safety to arrays

2017-04-16 Thread Dave Abrahams via swift-evolution
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

Re: [swift-evolution] [Pitch] Adding safety to arrays

2017-04-14 Thread Karl Wagner via swift-evolution
> 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

Re: [swift-evolution] [Pitch] Adding safety to arrays

2017-04-14 Thread Douglas Gregor via swift-evolution
> 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

Re: [swift-evolution] [Pitch] Adding safety to arrays

2017-04-13 Thread Paweł Wojtkowiak via swift-evolution
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

Re: [swift-evolution] [Pitch] Adding safety to arrays

2017-04-13 Thread Xiaodi Wu via swift-evolution
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

Re: [swift-evolution] [Pitch] Adding safety to arrays

2017-04-13 Thread David Sweeris via swift-evolution
> 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

Re: [swift-evolution] [Pitch] Adding safety to arrays

2017-04-13 Thread Tony Allevato via swift-evolution
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

Re: [swift-evolution] [Pitch] Adding safety to arrays

2017-04-13 Thread Brent Royal-Gordon via swift-evolution
> 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

Re: [swift-evolution] [Pitch] Adding safety to arrays

2017-04-13 Thread Jeff Kelley via swift-evolution
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"] >

Re: [swift-evolution] [Pitch] Adding safety to arrays

2017-04-13 Thread David Sweeris via swift-evolution
> 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

Re: [swift-evolution] [Pitch] Adding safety to arrays

2017-04-13 Thread Josh Parmenter via swift-evolution
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

Re: [swift-evolution] [Pitch] Adding safety to arrays

2017-04-13 Thread Lucas Neiva via swift-evolution
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,

Re: [swift-evolution] [Pitch] Adding safety to arrays

2017-04-13 Thread David Sweeris via swift-evolution
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

Re: [swift-evolution] [Pitch] Adding safety to arrays

2017-04-13 Thread Lucas Neiva via swift-evolution
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.

Re: [swift-evolution] [Pitch] Adding safety to arrays

2017-04-13 Thread Jeff Kelley via swift-evolution
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 =

Re: [swift-evolution] [Pitch] Adding safety to arrays

2017-04-13 Thread David Sweeris via swift-evolution
> 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” >