Re: Value of type '[AnyObject]!' has no member 'Generator'
Re: Value of type '[AnyObject]!' has no member 'Generator'
- Subject: Re: Value of type '[AnyObject]!' has no member 'Generator'
- From: Jens Alfke <email@hidden>
- Date: Mon, 05 Oct 2015 11:42:47 -0700
> On Oct 4, 2015, at 4:53 PM, Graham Cox <email@hidden> wrote:
>
> The explanation as to why Swift doesn’t suck doesn’t fit any notion in compuer science that I have ever heard of in almost 40 years of programming.
https://en.wikipedia.org/wiki/Option_type
Optionals (under various names) have been in use in functional languages for a long time, e.g. Haskell, OCaml, Scala, ML. They’ve been added to Java recently and are being proposed for C++.
Optionals are, among other things, a way to bring NULL pointers (which Tony Hoare, who arguably invented them, has called a “billion-dollar mistake”*) under control by adding them to the language’s type system. Null pointers are like the language saying “here’s a value, but it might crash your program if you try to use it without checking it first.” Optionals are like “here’s a value, and you have to check it first before using it, or you’ll get a compile error.”
They do add new and exciting compile errors, but you can think of those as substituting for runtime errors/crashes that could be a lot harder to debug. You can think of the “!!” situation as the compiler saying “you dereferenced a property of a possibly-nil object, and the property value is an array that itself might be nil; how are you going to handle those cases?"
> Maybe it’s just the jargon that needs a clearer explantion (I suspect it is), but when things are as clear as mud as this, you are not going to see the adoption rates you want.
Most of the pain around optionals comes from the Objective-C bindings. Gluing an API from one language into another language is always difficult; getting it to work well takes time and a lot of custom tweaking of the bindings. That’s been a work in progress over the 15 months that Swift has been publicly available.
The problems in this thread seem to stem from missing or incorrect annotations in one Cocoa class (CIFilter IIRC; I don’t have the whole thread in front of me.)
Even if you’re not using Swift, you’re benefiting from it because the Cocoa API annotations (and new Obj-C features to support them) are adding value for Objective-C developers too. The nullability annotations and collection item type-checking will help catch more errors at compile time. I’ve already had the compiler ding me a few times for passing nil to an API that doesn’t allow it.
—Jens
* http://developers.slashdot.org/story/09/03/03/1459209/null-references-the-billion-dollar-mistake
_______________________________________________
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