Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> Apple was one of the companies at the time that recognised the need for a safer, modern alternative to these languages. While no amount of compiler features can prevent you from introducing logic errors, they believed programming languages should be able to prevent undefined behavior, and this vision eventually led to the birth of Swift: a language that prioritized memory safety.

i'm pretty sure that's not the reason for the birth of Swift. Trying to access the 20th element from an array of 2 elements would cause a crash in almost all languages including Objective-C which Swift replaced.



Trying to access the 20th element in an array of 2 elements in C, or objective C for that matter, may instead of crashing, print out a security key, or give someone access to your credit card information. Security bugs are order of magnitude more dangerous than availability bugs. The industry is finally realizing that as we mature software engineering as a proper discipline. Human beings will write bugs, that's just a fact of life, we need to make sure the impact of those bugs is as small as possible. And the best tools we have for that, is programming languages where entire classes of bugs don't even exist anymore.


> Trying to access the 20th element from an array of 2 elements would cause a crash in almost all languages including Objective-C

It depends on which part of objective C you’re referring to: the C part, or the objective part. ObjC has NSArray, which has safe, bounds checked accessors. But ObjC is a strict superset of C, and C has very unsafe C-style arrays. In the latter, you definitely don’t always get a simple crash for accessing out of bounds… you get UB and buffer overflow exploits, same as C.


Yeah, people that shit on memory safety don't understand that memory bugs don't just lead to your program crashing. That has never been a problem. The problem is that they lead to you having to roll out fixes in a hurry in a race against hackers, with the company and safety of customer data on the line. A program crash is just an availability issue, a security bug can be an existential risk for a company.


Right. To elaborate on my original point, because ObjC includes C, saying "ObjC has safe arrays because it crashes on OOB" is both true and irrelevant. It has safe arrays, but that's not all it has. It also has very, very unsafe arrays. Swift doesn't have this issue, there is no unsafe array type in Swift (absent doing very unsafe byte-level address casting using functions that are literally prefixed with the word "unsafe".)


This part of the article is actually backwards. The interesting basic part about Swift isn't that it has bounds checks on arrays (which everyone also does), it's that it also has bounds checks on integer overflow, which half the "safe" languages turn into silent logic errors.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: