Thursday, August 27, 2015

Swift 2 Beta 6

Russ Bishop:

A new try? keyword has been added. This attempts an operation that may throw (fail). If it succeeds the result is wrapped in an optional. If it fails the error is ignored and nil is returned. This seems like a pragmatic compromise but I have to imagine someone lost a battle somewhere because a lot of programmers are going to slap a try? on it and ignore all errors.

This is a good addition that was suggested last month. I’m not worried about errors being ignored because the compiler warns if the result of try? is unused. It lets you ignore the specific error that you got, but you still have to handle the fact that there was an error.

Variadic parameters can appear anywhere in the parameter list; presumably the compiler disambiguates them by type and/or by consuming values off the right-hand side of argument list until the non-variadic arguments are satisfied.

It looks to me like it’s simpler than that, and you simply use a named parameter to signal the end of the variadic parameter. Perhaps in the future it will be possible to support multiple variadic parameters in this way. For now, the main benefit is that you can more easily use trailing closures.

extend was renamed appendContentsOf and splice was renamed insertContentsOf

It’s good to see Apple keeping some of Foundation’s good ideas.

Most APIs that take closures or @autoclosure parameters use rethrows properly so you can pass throwing methods to map, filter, etc. It also allows short-circuiting operators like &&, ||, or ?? to work with expressions that throw.

This should make error handling easier than before.

There are still some bugs, though:

Declaring multiple globals in a single var or let may cause corruption of their values; the workaround is to declare them with separate var or let statements.

Erica Sadun:

You can combine try? with ?? for fallback values but you must surround the try in parens[…]

Erica Sadun:

Beta 6 introduces a lazy property on sequences.

[…]

This property is implemented in CollectionType, LazyCollectionType, and LazySequenceType extensions. You use it to delay evaluation on things like map and filter that are normally eager.

Erica Sadun:

Join has become joinWithSeparator, which you call after a sequence, interposing a separator between the elements of a sequence.

1 Comment RSS · Twitter


[…] Swift 2 Beta 6; Up the mix with Beta 6; How to Print: the Beta 6 edition […]

Leave a Comment