Beta 4: We want more! #swiftlang

Just a few quick notes of interest

Falling in love with indirect enums. You can declare a single case indirect or the entire enum as a whole. Still a few fiddly bits. Trying to do the pattern match / variable binding in an if statement kills the compiler.

enum List<T> {
    case Nil
    indirect case Cons(head: T, tail: List<T>)

    func dumpIt() {
        switch self {
            case .Cons(head: let car, tail: let cdr):
                print(car); cdr.dumpIt()
            default: break
       // Fail, die, Signal Abort trap 6
       // if case .Cons(head: let car, tail: let cdr) = self {
       //     print(car); cdr.dumpIt()
       // }

// Construct a list and dump it
var node = List<Int>.Nil
for value in [1, 2, 3, 4, 5] {
 node = List.Cons(head: value, tail: node)

The very odd single-uple thing where you could refer to something by .0 is now gone. The ability to refer to the “0” element of a scalar value (producing the scalar value itself) has been removed. (17963034)

SinkType and SinkOf are now gone. RIP.

Extensible collections are folded into Range-replaceable, and there are some nice new default implementations.

You can now use “performSelector“! Well, to a point. You have to descend from NSObject, but hey, why not?

// Sorry, not going to work here
extension Int {func plus1()->Int {return self + 1}}

// IntClass does not have a member named performSelector
// unless you descend from NSObject
class IntClass : NSObject {
    var value : Int = 0
    func plus1() {value = value + 1}

//let z = 5.performSelector(Selector("plus1"))
//print(z) // DIES!

let w = IntClass(); w.value = 5
print(w.value) // prints 6, works

Throws annotation is now official  Swift documentation comments recognize a top-level list item – Throws: … which should be used to document what errors can be thrown and why. These appear alongside parameters and return descriptions in Xcode’s QuickHelp. (21621679)

You can now conform to protocols that are “less available than the type itself”, so a class that’s available from say iOS 6 can conform to a protocol that’s specific to iOS 9.

Mirrors are messed up. Total redesign and far less functionality plus lots and lots of bugs. I’ll worry about this next week but right now don’t expect anything to work the way it did or you might expect it to. For example, mirrors of scalars seem to lack a simple “.value” option.

func ExploreItem(item: Any) {
    func ExploreItem_(item: Any, indent: Int) {
        let mirror = Mirror(reflecting: item)
        print(String(count: indent, repeatedValue: Character(" ")), appendNewline:false)
        print("\(mirror.description) : \(mirror.subjectType)", appendNewline:false)
        if mirror.displayStyle != nil {
            print("[\(mirror.displayStyle)]", appendNewline: false)}
        let count = mirror.children.count
        if count != 0 {
            print(" \(count) children", appendNewline:false)}
        if count == 0 {
            print(String(count: indent + 4, repeatedValue: Character(" ")), appendNewline:false)
            print("- \(item)")
       for child in mirror.children {
           print(String(count: indent + 4, repeatedValue: Character(" ")), appendNewline:false)
           let label = child.label ?? "??"
            print("- \(label): \(child.value)")
    print("Exploring \(item)")
    ExploreItem_(item, indent: 0)

More as I find it.


  • The old mirror API is still available as _MirrorType/_mirror(), hopefully it won’t be removed entirely before the new one is more caught up

  • What does (17963034) and (21621679) stand for?