THE BLOG
06/05/2014 01:22 pm ET Updated Aug 05, 2014

Hello Swift, Meet Scala

Yesterday, during a four and a half hour flight, I decided to read the Swift programming book (because....why not?). I was immediately blown away by Swift's similarities to Scala and think that there have been massive improvements from Objective-C.

UITextView *view = [[UITextView alloc] init] becomes something like

var view: UITextView

After going through the book, I decided to take some notes on a few of the more interesting similarities. I think it's a great testament to some of the features of Scala (granted, there are plenty of influences from Python, Ruby, etc.). Below are some features in Swift that have a sister presence in Scala. The Swift example is listed first, and Scala is listed second.

Options - Swift supports the optional type and removes the nil concept the same way scala removed the noise of dealing with null

var myString: String?
val myString: Option[String]

Case Pattern Matching (with Guards !!) - One of Scala's most powerful features is pattern matching, and the case pattern matching shows up in Swift, with guards

switch value {
case (x) where x > 0 : "positive"
case(x) where x < 0 : "negative"
}

//scala
value match {
case x if x > 0 => "positive"
case x if x < 0 => "negative"
}

Tuples - A data structure that allows you to return groups of data is present as well.

var x: (String,String) = ("dog","cat")
val x:(String,String) = ("dog","cat")

Underscore - We all know the underscore has 11 meanings in Scala, but it's cool to see it used again here

var x
(x,_) = ("Some var", "Who cares!")

//scala
List((1,2)).map { case (x,_) => println(x) }

Type Alias - Just as in Scala, you can easily create custom aliased types for easier code readability and clarity

typealias myString = String
type myString = String

Higher Order Functions - You can pass functions as params, return functions from functions, etc.

func returnFunc() -> (Int -> Int)

def returnFunc: Int => Int

Variadic Parameters - You can pass an arbitrary number of args to a method/function. I think the triple dots is my least favorite semantic notation of Swift, it looks like a typo or an example.

func unlimitedString(x: String...)

def unlimitedStrings(x: String*)

Function Types - A function type has a definition of its parameters and return type and that can easily be assigned to a var

func myFunc(x: Int) -> Int
var x: Int -> Int = myFunc
x(2)

def myFunc(x: Int): Int
val x = myFunc _
x(2)

Lazy Properties - Just like Scala, you can defer the loading of a var until it's requested

@lazy var x = complexFunction()

lazy val x = complexFunction()

Any Type Casting - Similar to Scala, Swift has the Any object which is a superclass of all objects that you can use to represent any type

var x:Any = 5

val x:Any = 5

Extensions - These kind of feel like traits but they are explicitly mapped to a specific class or structure or enum, where you can extend the functionality of existing objects, but they can not be instantiated on their own

extension SomeType {
var convert(x: Int): Int = { return x * 2 }
}

trait myTrait {
def convert(x: Int) = x *2
}

Protocols - These kind of feel like abstract classes or traits, in that new classes "implement" these protocols and there is no defined implementation in them. They can define the abstract types required for implementation and classes define their conformity to them.

protocol huffProto {
var requiredParam: Int {get }
func needMe() -> Int
}

class myClass: huffProto { .....

//scala

abstract class huffProto {
val requiredParam: Int
def needMe: Int
}

class myClass extends huffProto {....

Generics - Another great concept that is very cool to see in Swift. Generics allow you to define methods or classes with arbitrary data types to reduce the boilerplate of objects that operate the same way but just on different types

struct MyStruct {
var items: T[]()

fund add(item: T) {
items.append(item)
}
}

abstract class myClass[T] {
var items: Seq[T]

def add(item: T) =
items :+ item
}