Swift: Inspired by Scala?
As part of WWDC this year in San Francisco, Apple announced a new programming language for iOS called Swift.
I took a look at the Swift language guide and I was surprised to see that the language has been heavily influenced by Scala. But then, what language isn’t being heavily influenced by Scala lately? Even Java 8 has added Scala like features in the form of Lambda expressions.
Never the less, I was pleased with many of the choices that Swift has made. It is certainly a huge leap forward from Objective-C, and a long overdue one. The language looks awesome.
Scala | Swift | |
---|---|---|
Constants | val max = 10 | let max = 10 |
Variables | var min = 5 | var min = 5 |
Type Annotation | var welcome: String | var welcome: String |
Printing | println(welcome) | println(welcome) |
Semicolons | Optional | Optional |
Comments | // Single Line /* * Multi-Line */ |
// Single Line /* * Multi-Line */ |
Type Safety | Yes | Yes |
Type Inference | Yes | Yes |
Type Alias | type AudioSample = Int | typelias AudoSample UInt16 |
Tuples | val http404Erro = (404, "NotFound") | let http404Erro = (404, "NotFound") |
Optionals | val name: Option[String] | val name = String? |
Unwrapping Optionals | name.get | name! |
Optional Binding | val converted = num.getOrElse(default) | if let converted = num { let converted = default } |
Ranges | 1 to 10 | 1...10 |
Ranges | 1 to 10 | 1...10 |
String Interpolation | val message = s"Some $value" | let message = "Some \(value)" |
Array Literals | val arr = Array("Hello","World") | let arr = ["Hello", "World"] |
Dictionaries | var airports = Map(("TYO", "Tokyo"), ("DUB", "Dublin")) | var airports: Dictionary<String, String>= ["TYO": "Tokyo", "DUB": "Dublin"] |
Accessing Dictionaries | airports("TYO") | airports["TYO"] |
Iterating Dictionaries | for ((airportCode, airportName)<- airports) | for (airportCode, airportName) in airports { ... } |
Switch with Range | t match { case x if ((0 to 10).contains(x)) =>true ; case _ =>false } | switch t { case 0...10 return true; default: return false } |
Named Parameters | def printName(first:String, last:String) { println(first + " " + last) } | func printName(#first: String, #last: String) { ... } |
Default Parameter Values | func join(start, middle = " ", end) | func join (start, middle = " ", end) |
Generics | func concat<T>(a: T, b: T) | func concat<T>(a: T, b: T) |
Extension / Mixins | trait Mixin { var i: Double; def doSomething() { } } |
extension Mixin { var i: Double; doSomething() { } } |
As you can see, other than the change from val to the pascal like let, many of the language features are exactly the same. It is great to see Apple finaly come to the table with a modern language design. Now if only Google would step up and bring Java 8 features to Android. Google recently added support for Java 7 features features if you are using Gradle and jump through some hoops, though try with resources is only supported on API 19 and above due to the need for support in the VM. Considering that Oracle released version 7 in July 2011, so we can expect Google to add Java 8 features in 2017! ;)
I am looking forward to using Swift to make my iOS code suck less to write.