Last week in Swift Evolution
Week of Dec 28, 2015
Published on
Happy New Year, everyone. Here’s a summary of selected updates from last week from the Swift Evolution repo and mailing list:
Up for review this week
-
New keyword for associate type declaration
We declare associated types currently using the
typealias
keyword, and the same keyword is used to create an alias for a type. Loïc Lecrenier proposed a new keyword to declare associated types. The keyword was initiallyassociated
, and has now been changed toassociatedtype
.This proposal is up for review now (Jan 4 – Jan 6). This is quite a straightforward proposal (the main debate earlier was about what the new keyword should be named) and looks likely to be accepted.
-
The Swift Package Manager team had proposed to include tests in Swift packages. The package manager looks for tests within a
Tests
subdirectory, and the tests can be run with aswift build --tests
command.This proposal is scheduled for review this week (Jan 5 – Jan 7).
-
Flexible memberwise initialization
Swift currently offers an automatic memberwise initializer for structs (unless they have an explicit initializer). Matthew Johnson proposed a way for this feature to be made available for classes (under certain conditions), and also allows us to specify values for private variables in the memberwise initializer (among other stuff). The proposed syntax looks like this:
class MyClass { var i: Int private var j: Int memberwise init(...) { // '...' is part of the syntax j = 42 } } let m = MyClass(i: 2)
This proposal is scheduled for review this week (Jan 6 – Jan 10).
Discussions
These are the discussions from last week that I found the most interesting:
-
Automatic protocol forwarding (last week)
Per this draft proposal from Matthew Johnson, we can have function calls on a type (the forwarding type) automatically forwarded to a member (of type forwardee) of the forwarding type.
-
Generalized naming for any function (previously here, last week)
Swift functions can be assigned to variables, but when the same base name is shared by multiple functions (like
insertSubview(view:UIView, aboveSubview: UIView)
andinsertSubview(view: UIView, belowSubview: UIView)
), Swift doesn’t have a way for us to specifically name a particular function. Doug Gregor had proposed to name functions like this, and extended it to support setters, getters, initializers and subscripting.Last week, Joe Groff suggested that the same idea can be extended to disambiguate function calls (whether to call
foo()
inSuperProtocolA
or inSuperProtocolB
). -
Expression to retrieve the Objective-C selector of a method (earlier, last week)
In a discussion related to the previous one, here’s a pitch to fix the “stringly typing” when we access Obj-C selectors in Swift. So, instead of the brittle
btn.addTarget(self, action:"foo", ...)
, we can say something like:btn.addTarget(self, action:Selector(MyClass.foo), ...)
. -
Kevin Ballard suggested a bunch of additions to the Swift standard library:
- CollectionType.cycle: Returns an infinite sequence looping through a finite collection
- SequenceType.find(): Returns the first element matching a predicate
- BufferedSequence, BufferedGenerator: Wrapper around a sequence / generator that lets us peek into it without modifying its index
Other
- The Swift team published a list of “commonly rejected” ideas
- Chris Lattner explained his thoughts on what stuff should go into the Swift standard library and what into the Swift foundation