Re: Stupid ! and ?
Re: Stupid ! and ?
- Subject: Re: Stupid ! and ?
- From: Jens Alfke <email@hidden>
- Date: Sat, 25 Apr 2015 12:03:35 -0700
> On Apr 25, 2015, at 7:59 AM, William Squires <email@hidden> wrote:
>
> 1) Explicitly state that object references must be tested for nil before use (like C,C++, etc…),
Because it’s extremely unsafe and leads to crashes or heap corruption, and at worst, catastrophic security holes. If you’re keeping up with the almost-daily announcements of security problems in major pieces of software, it should be clear that we can’t keep going on this way.
> 2) Use the ObjC behavior that sending a message to nil does nothing (unless the message returns something other than void, in which case you'll get some all-zero-bits equivalent return value, and you can still test for nil if you want)
Because it’s still unsafe, although slightly less so, and leads to bugs that are difficult to track down because the point of noticeable failure happens after (sometimes long after) the unexpected nil result.
A major part of the reason Swift’s ?/! stuff feels annoying is because we’re using Swift with an API that wasn’t designed for it, and where the bindings are still in the process of being improved.
BTW, this design is _not_ new to Swift; the idea of using smart enums with pattern-matching comes mostly from functional languages like Haskell where there’s a longstanding belief that null pointers are harmful. It’s also being adopted by other new not-purely-functional languages like Rust and (IIRC) Nim.
—Jens
PS: Speaking of Rust, I think it’s a much more interesting language than Swift. Part of the reason I haven’t started doing serious coding in Swift yet is because I’m both waiting for Rust 1.0 [a month or two away], and wondering whether Apple will decide to open-source Swift and make it a viable language from cross-platform programming.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden