<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=""><br class=""><div><blockquote type="cite" class=""><div class="">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 style="font-family: Helvetica; font-size: 12px; 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-stroke-width: 0px;" class=""></div><div style="font-family: Helvetica; font-size: 12px; 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-stroke-width: 0px;" class=""><div class=""><div id="bloop_customfont" style="font-family: Helvetica, Arial; font-size: 13px; margin: 0px;" class="">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" style="font-family: Helvetica, Arial; font-size: 13px; margin: 0px;" class=""><br class=""></div><div id="bloop_customfont" style="font-family: Helvetica, Arial; font-size: 13px; margin: 0px;" class="">var myString: String? = “hello”</div><div id="bloop_customfont" style="font-family: Helvetica, Arial; font-size: 13px; margin: 0px;" class="">myString.append(“ world!”)</div><div id="bloop_customfont" style="font-family: Helvetica, Arial; font-size: 13px; margin: 0px;" class=""><br class=""></div><div id="bloop_customfont" style="font-family: Helvetica, Arial; font-size: 13px; margin: 0px;" class="">The string is optional, not guaranteed to exist, so the last line requires a “!” to force-unwrap it.</div><div id="bloop_customfont" style="font-family: Helvetica, Arial; font-size: 13px; margin: 0px;" class=""><br class=""></div><div id="bloop_customfont" style="font-family: Helvetica, Arial; font-size: 13px; margin: 0px;" class=""><br class=""></div><div id="bloop_customfont" style="font-family: Helvetica, Arial; font-size: 13px; margin: 0px;" class=""><br class=""></div><div id="bloop_customfont" style="margin: 0px;" class=""><div id="bloop_customfont" style="margin: 0px;" class=""> public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {</div><div id="bloop_customfont" style="margin: 0px;" class=""> let section = self.sections[section]</div><div id="bloop_customfont" style="margin: 0px;" class=""> </div><div id="bloop_customfont" style="margin: 0px;" class=""> return section.items.count</div><div id="bloop_customfont" style="margin: 0px;" class=""> }</div><div id="bloop_customfont" style="margin: 0px;" class=""><br class=""></div><div id="bloop_customfont" style="margin: 0px;" class="">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" style="margin: 0px;" class=""><br class=""></div><div id="bloop_customfont" style="margin: 0px;" class="">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" style="margin: 0px;" class=""><br class=""></div><div id="bloop_customfont" style="margin: 0px;" class=""> let section = self.sections[section]</div><div id="bloop_customfont" style="margin: 0px;" class=""> if section == nil {</div><div id="bloop_customfont" style="margin: 0px;" class=""> return 0</div><div id="bloop_customfont" style="margin: 0px;" class=""> } else {</div><div id="bloop_customfont" style="margin: 0px;" class=""> return section!.items.count</div><div id="bloop_customfont" style="margin: 0px;" class=""> }</div><div id="bloop_customfont" style="margin: 0px;" class=""><br class=""></div><div id="bloop_customfont" style="margin: 0px;" class="">Or you could do this:</div><div id="bloop_customfont" style="margin: 0px;" class=""><br class=""></div><div id="bloop_customfont" style="margin: 0px;" class=""> let section = self.sections[section]!</div><div id="bloop_customfont" style="margin: 0px;" class=""> </div><div id="bloop_customfont" style="margin: 0px;" class=""> 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>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><br class=""></div><div>- Dave Sweeris</div></body></html>