Swift first impressions

:  ~ 1 min read

Technically, it's not a first impression, because the first one was "ugh". But then I read the book and watched all the WWDC videos. Oh boy!

Don't even know where to start, but man, oh man, some stuff is just mind-blowing. Here are a few examples of Operator Overload combined with Generics.

// "test" * 2
func * (left: String, right: Int) -> String {
    var newString = ""
    right.times {
        newString += String(left)
    }
    return newString
}
// testtest

// "abcdef"[2]
subscript(digitIndex: Int) -> Character? {
    var i = 0
    for char in self {
        if i == digitIndex {
            return char
         }
        i++
    }
    return nil
}
// c

// [1, 2, 3] << 4
func <<<T> (inout left: T[], right: T) {
    left.append(right)
}
// [1, 2, 3, 4]

// view << newSubview
func <<<T: UIView> (inout left: T, right: T) {
    left.addSubview(right)
}

// emptyDic = ["k1": "v1", "k2": "v2"] + ["k3": "v3"]
func +<K, V> (left: Dictionary<K, V>, right: Dictionary<K, V>) -> Dictionary<K, V> {
    var l = left
    
    for (k, v) in right {
        l[k] = v
    }
    
    return l
}
// emptyDic = ["k1": "v1", "k2": "v2", "k3": "v3"]

// emptyDic << ["k4": "v4"]
func <<<K, V> (inout left: Dictionary<K, V>, right: Dictionary<K, V>) {
    left += right
}
// emptyDic = ["k1": "v1", "k2": "v2", "k3": "v3", "k4": "v4"]

As for iOS 8 goodies ... Check NSHispter's latest post.