<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">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:<div class=""><br class=""></div><div class=""><font face="Menlo" class="">let array = ["foo", "bar", "baz"]</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class="">let first = array[0] // type is `String`</font></div><div class=""><font face="Menlo" class="">let third = array[2?] // type is `String?`, value is .some("baz")</font></div><div class=""><font face="Menlo" class="">let fourth = array[3?] // type is `String?`, value is .none</font></div><div class=""><br class=""><div class="">
<div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant-ligatures: normal; font-variant-position: normal; font-variant-caps: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant-ligatures: normal; font-variant-position: normal; font-variant-caps: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant-ligatures: normal; font-variant-position: normal; font-variant-caps: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant-ligatures: normal; font-variant-position: normal; font-variant-caps: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant-ligatures: normal; font-variant-position: normal; font-variant-caps: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; border-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-stroke-width: 0px;"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Jeff Kelley</div><div class=""><br class=""></div><div class=""><a href="mailto:SlaunchaMan@gmail.com" class="">SlaunchaMan@gmail.com</a> | <a href="https://twitter.com/SlaunchaMan" class="">@SlaunchaMan</a> | <a href="http://jeffkelley.org" class="">jeffkelley.org</a></div></div></span></div></div></div></div></div>
</div>
<br class=""><div><blockquote type="cite" class=""><div class="">On Apr 13, 2017, at 8:19 AM, David Sweeris via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="font-family: Helvetica; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" class=""><div class=""><br class="Apple-interchange-newline">On Apr 13, 2017, at 3:56 AM, Andrew Hart via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"></div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><div class=""><div id="bloop_customfont" class="" style="font-family: Helvetica, Arial; font-size: 13px; margin: 0px;">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:</div><div id="bloop_customfont" class="" style="font-family: Helvetica, Arial; font-size: 13px; margin: 0px;"><br class=""></div><div id="bloop_customfont" class="" style="font-family: Helvetica, Arial; font-size: 13px; margin: 0px;">var myString: String? = “hello”</div><div id="bloop_customfont" class="" style="font-family: Helvetica, Arial; font-size: 13px; margin: 0px;">myString.append(“ world!”)</div><div id="bloop_customfont" class="" style="font-family: Helvetica, Arial; font-size: 13px; margin: 0px;"><br class=""></div><div id="bloop_customfont" class="" style="font-family: Helvetica, Arial; font-size: 13px; margin: 0px;">The string is optional, not guaranteed to exist, so the last line requires a “!” to force-unwrap it.</div><div id="bloop_customfont" class="" style="font-family: Helvetica, Arial; font-size: 13px; margin: 0px;"><br class=""></div><div id="bloop_customfont" class="" style="font-family: Helvetica, Arial; font-size: 13px; margin: 0px;"><br class=""></div><div id="bloop_customfont" class="" style="font-family: Helvetica, Arial; font-size: 13px; margin: 0px;"><br class=""></div><div id="bloop_customfont" class="" style="margin: 0px;"><div id="bloop_customfont" class="" style="margin: 0px;"> public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {</div><div id="bloop_customfont" class="" style="margin: 0px;"> let section = self.sections[section]</div><div id="bloop_customfont" class="" style="margin: 0px;"> </div><div id="bloop_customfont" class="" style="margin: 0px;"> return section.items.count</div><div id="bloop_customfont" class="" style="margin: 0px;"> }</div><div id="bloop_customfont" class="" style="margin: 0px;"><br class=""></div><div id="bloop_customfont" class="" style="margin: 0px;">In this example, we could provide a section number that goes beyond the bounds of the self.sections array, without any warning.</div><div id="bloop_customfont" class="" style="margin: 0px;"><br class=""></div><div id="bloop_customfont" class="" style="margin: 0px;">My suggestion is perhaps arrays should by default return an optional when given an index, and of course they’d support forced-unwrapping too. So you could then do this:</div><div id="bloop_customfont" class="" style="margin: 0px;"><br class=""></div><div id="bloop_customfont" class="" style="margin: 0px;"> let section = self.sections[section]</div><div id="bloop_customfont" class="" style="margin: 0px;"> if section == nil {</div><div id="bloop_customfont" class="" style="margin: 0px;"> return 0</div><div id="bloop_customfont" class="" style="margin: 0px;"> } else {</div><div id="bloop_customfont" class="" style="margin: 0px;"> return section!.items.count</div><div id="bloop_customfont" class="" style="margin: 0px;"> }</div><div id="bloop_customfont" class="" style="margin: 0px;"><br class=""></div><div id="bloop_customfont" class="" style="margin: 0px;">Or you could do this:</div><div id="bloop_customfont" class="" style="margin: 0px;"><br class=""></div><div id="bloop_customfont" class="" style="margin: 0px;"> let section = self.sections[section]!</div><div id="bloop_customfont" class="" style="margin: 0px;"> </div><div id="bloop_customfont" class="" style="margin: 0px;"> return section.items.count</div></div><div id="bloop_sign_1491894931246087936" class="bloop_sign"></div><div id="bloop_sign_1491894931246087936" class="bloop_sign"><br class=""></div><div id="bloop_sign_1491894931246087936" class="bloop_sign">Of course this would be less convenient in a lot of cases, but this is the 1 place where apps seem to encounter a crash, crashing for the same reason that’s especially avoided across most of the rest of Swift.</div></div></div></div></blockquote><br class=""></div><div style="font-family: Helvetica; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">My understanding is that we need the current behavior to meet performance goals. We’ve discussed adding a “safe” subscript before, but the discussion usually fizzles out when no clear winner for the argument label emerges.</div><div style="font-family: Helvetica; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">- Dave Sweeris</div><span style="font-family: Helvetica; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">_______________________________________________</span><br style="font-family: Helvetica; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">swift-evolution mailing list</span><br style="font-family: Helvetica; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="mailto:swift-evolution@swift.org" style="font-family: Helvetica; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class="">swift-evolution@swift.org</a><br style="font-family: Helvetica; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" style="font-family: Helvetica; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></div></blockquote></div><br class=""></div></body></html>