Swift 2, Beta 2
Quick Recap
June 23, 2015
A quick recap for those who haven't parsed the release notes yet:
New Features
- Enum cases with values can be used as a function; basically the compiler automatically creates a function that accepts parameters matching the case's values and returns that enum case with the values filled in.
- Non-mutating methods of structs, enums, and protocols can be partially applied to their
self
parameter. This is just a fancy way of saying if you passType.method
you'll get the uncurried implementation that needs an instance to operate on and if you passinstance.method
you'll get the curried implementation that hasself
filled-in. This already worked for reference types but value types are trickier which is why the non-mutating requirement is there. - Initializers can be grabbed as a property on the type, e.g.:
String.init
. You can still omit it when creating a new value, but if you want to treatinit
as a function you can pass around or if you want toinit
a type object you'll need to use it explicitly. - You can grab a reference to a generic parameter's instance methods just like any reference type, so
A.method
infunc smash<A: SomeProtocol>
now works.
These are all things that let you access various parts of Swift as if they were standard functions which is a nice win for consistency.
Notable Fixes
- The Swift compiler has assertions enabled so crash reports should be more useful; not sure why this wasn't part of the first beta.
- Various crashes at compile time and runtime were fixed
- Extensions to generic types can add constraints on the type parameter in the declaration of the extension itself which is a nice simplification
- Overriding an Objective-C method in a way that can't be represented in Objective-C now produces a diagnostic (👍🏻)
- It looks like Protocol metatypes are workable now but I have yet to confirm this.
Loading, Please Wait...
indirect
enums haven't landed yet (this is the new feature that lets an enum case contain a value that is the enum itself)- Extending a CF type to conform to a protocol will fail or crash at runtime in optimized builds
guard
doesn't bind variable names at the top level (a Playground, main, etc) so the values bound in the conditional check don't appear beyond theguard
like they should
Conclusion
Some handy changes in Beta 2 but I wouldn't expect much response to WWDC feedback and bug reports until the Beta 3-4 timeframe.
Up next: SIMD and Swift
Russ Bishop
This blog represents my own personal opinion and is not endorsed by my employer.