5 ms·
extension Collection { subscript (safe index: Index) -> Element? { return indices.contains(index) ? self[index] : nil } } And now you have the
by zoul 8y ago
extension Collection {
subscript (safe index: Index) -> Element? {
return indices.contains(index) ? self[index] : nil
}
}
And now you have the choice:
array[i] // traps for invalid indices
array[safe: i] // returns nil for invalid indices
- valuearb 8y agoLol, I put that code in my last project, a 150,000 line codebase built by lazy contractors, as just one of the tools to clean up the errors from their objective c like approach to software quality.
- blub 8y agoInterestingly put, "the Objective-C approach to software quality". There's something in the psychology of a large number of C, Objective-C and C++ programmers that makes them overappreciate execution speed and underappreciate memory safety in particular, but also validation and correctness in general. One of my goals now is to outline safe ways of using C++ for various projects and I'm not too optimistic. In fact I'm unsure what we'll end up with.
- pjmlp 8y agoQuite true, hence why I say that the problem is human and not technical. Those of us that come from Wirth languages always put quality and delivering what the program is expected to do before going crazy with optimizations. I always got the feel that many in the C communities write code as if they were micro-optimizing every single line, just because. And those that care about type safe driven programming in C++ usually have background in safer languages.
- fauigerzigerk 8y agoI don't think Swift has a way to catch all runtime errors that others may have introduced in a reliable way. Perhaps because it's not a huge problem in iOS apps, but for long running multi-threaded applications this is a problem.
- fauigerzigerk 8y agoI always have a choice if I'm the one writing the code. But what I want is a way to catch all errors that others may have introduced in code I have no control over. That includes arithmetic overflow as well.
- zoul 8y agoWhat would you do? If the ordinary array subscript produced an optional value, people would simply fall into the habit of force-unwrapping it, since the hassle of handling the spreading optionals would be too big. And throwing an unchecked exception would lead to empty catch blocks everywhere, just as in Java. And then the execution would continue in God only knows what state. I simply don’t think that the type system can solve the question of trusting your dependencies for you.
- fauigerzigerk 8y agoWhat I want to do is to wrap a piece of code running in its own thread in a catch block so I can respond gracefully to any errors without killing the entire process. What gracefully means depends entirely on the application. Often it means to log the error or display a meaningful error message if it's an interactive app, then maybe do other kinds of cleanup and shut the thread down. It can also mean to retry later. Array out of bounds errors are quite often just a result of not properly handling an earlier temporary error. Whether or not errors leave the rest of the application in an inconsistent state depends entirely on the design of the application. I'm not saying that letting an app crash on buggy code is never the right thing to do. I just want the option to handle it differently where appropriate and possible (it's not always possible anyway).
- zoul 8y agoYou might be interested in this: https://gist.github.com/lattner/31ed37682ef1576b16bca1432ea9f782 https://gist.github.com/lattner/31ed37682ef1576b16bca1432ea9... Speaking of reliable systems, introducing an actor model is a good opportunity and excuse to introduce a mechanism for handling and partially recovering from runtime failures (like failed force-unwrap operations, out-of-bounds array accesses, etc). We explore several options that are possible to implement and make a recommendation that we think will be a good for UI and server applications. This looks like a more principled approach that we will eventually get in Swift.