The Swift language, successor to Objective-C

To replace Objective-C, Apple has designed a new language, capitalizing on advances in the field of programming languages, to speed up development and execution time.

The language has been influenced according to the authors by C#, Objective-C, Haskell and many others (see the description of the syntax below). It is based on LLVM tools and produces native code.

Sullivan sur Swift et Go

Bryan O'Sullivan about Swift and Go.

A comment on Twitter by a specialist of the Haskell language (image at right) compares it to Go. The comparison comes naturally in mind because Swift wants to succeed to Objective-C while Go wants to do the same to C++.
Actually Swift takes some syntax elements from Go:

However, it differs from Go on many key aspects, including the use of inheritance when Go uses composition, concurrency is a library for the former, the latter lacks genericity. Go further is a minimalist language (only one loop construct) while Swift aims to integrate all possible elements to each aspect of the language.

Swift is propietary and it is not open source for now, and it requires XCode to make a program. It comes immediately with extensive documentation, a complete tutorial and a playground as TypeScript to test the code. In fact its design began in 2010 (Ref.), that left four years to prepare its public distribution.

Language features...

Syntax description

Displaying Hello World!

println("Hello World!") 

Variables may be inserted into a string with the operator \() that unlike $ in PHP allows to mark the end of the variable name.

Type defined by inference

As in Go, Julia, and many scripting languages.

var x = 10

The integer value assigned gives to x the type Int. The type can not change as it would be the case with a dynamic language.

Explicit type

It has become common that variables are defined by var and the type specified as an option, but as far variables are not dynamic as in Julia.

var s = "hello : string

Constant

Constants are defined by let. We do not see the advantage over the word const and let has a different meaning in other languages ​​like JavaScript or Basic that originally brought the word.

let x = 10

Tuple

A tuple (inspiration from Python) can be displatched automatically to provide multiple returns to functions.

let tuple1 = (1000, "hello") 
let (x, y) = tuple1

x get the value 1000 and y the value "hello".

If control structure

Takes the principle of Go: no bracket, but mandatory braces:

if x == 10 {  
    println("equal to 10")
} else if x < 10 {
    println("less than 10")
} else {
    println("more than 10")
}

For loop

The loop in a range is similar to that of Ruby and Scriptol. Swift uses .. for an exclusive range, ... for an inclusive range and Ruby does the contrary. Scriptol uses .. for an inclusive range and -- for a exclusive range. Double points are used in typography to represent an interval that was also copied by Bash, while ellipsis is the symbol of an indefinite suite. The choice of scriptol is different because it does not use -- for decrement. Python uses the syntax of ALGOL 68.

for var i in 1 ... 10 {
    println(i)
} 

The format of C is hoawever also implemented to enable choice of the increment. We could instead add a step option like in Basic.

for var i = 0; i < 10; ++i {
}

For in applies to a collection, which is now implemented in all languages, even in C++.

for x in somelist {
    println("Item \(x)")
} 

The classic example to display the letters of a string is as simple in Swift as in Python.

for c in "demo" {
  println(c)
} 

A tuple is used to obtain the key-value pairs in a dictionary.

let liste = [ "a" : "apple", "b" : "orange", "c" : "berry" ] 

for (k , v) in liste {
    println("\(k) = \(v))
}

While and do while

while x <  y {
}
do {

} while x < y

Switch

It solves the issue of common processing to multiple cases by separating them with a comma. Like Go it can also use the reserved word fallthrough to chain clauses. Intervals are possible like in scriptol.
One can also use tuples in the cases but only to compare to an initial tuple.

switch x {
case 1:
    println("1")
case 2, 3, 4:
    println("between 2 and 4")
case 5 ... 10:
    println("between 5 and 10")
default :
    println("another value")
}

The break and continues clauses associated with a label in a switch inserted in a while loop makes is possible to jump to an instruction or move to the next iteration.

Pattern matching

The rules of the switch statement may contain conditions with the where clause. The principle of pattern matching goes back to the SNOBOL language (1962). Python got pattern matching through an extension named SnoPy.

switch x {
  case y where y < 10
  case z where x + z = 20
}

Function

The syntax is inverted with respect to C, as it is the fashion with new languages, ​​without providing nothing more. A function supports multiple returns, default arguments. You can nest functions within another function, that are inaccessible outside of it.

func myfunction( s : String) -> String {
   s += "suffix"
   return s
}

Multiple returns is achieved with a tuple. The name of the returned variable must be defined in both the header and the return instruction.

func pair(x : Int, y : Int) -> (a : Int, b : Int) {
  a = x  * 2
  b = y + 5
  return(a, b)
} 

Variadic parameters are demoted with three dots, inspired by C, which is relevant here.

func listing(x : Int ...) -> Int {
  var a, b, c 
  (a, b, c) = x
}
listing(5, 10, 20); 

The argument x is actually an array and but it does make a difference at the function call.

Classes and inheritance

Classes have single inheritance and multiple interfaces, such as in Java, C#. The constructor is called init rather than "constructor" or with the name as the class, just to be different.

class Vehicle {
   var speed : Int
   var name : String
   func getname() {
      return name
  }
}

class Car : Vehicle {
   Int passengers : Int
}

Protocol

Interfaces are called protocol to be original. They can be typed or generic. Inspired by C# with its getters and setters: the words get and set specify whether you can read or assign a property.

protocol moninterface {
  var i: Int { get set }
  var name: String { get }
}

An interface (protocol) is inherited like a class:

class car : moninterface {
  var name : String = "hello"
} 

Extensions

Traits here are called extensions. This allows you to add methods to existing classes. We see here the influence of Rust : changing the name of common elements of languages gives them an air of novelty!

extension tentimes {
  var up : Int { return self * 10 }
} 

var ten : Int = 10
println(ten.tentimes)

Who will use Swift ?

The adoption of Swift should not be a problem for developers on Apple's mobiles. They already use a language particular to the platform, Objective-C, and migrate to a more modern and compatible version can only simplify their lives.
LLVM bitcode production could potentially provide access to Android, but only if the application does not use Apple's framework and if XCode is no longer needed. The same restriction applies to other operating systems.
So, apart from the world of Apple, the interest of Swift is purely academic and to show that progress can be made in programming languages, or rather that companies can put them into practice.

Site: