Swift language, successor to Objective-C

To replace Objective-C, Apple has developed a new language that wants to leverage advances in languages as well as produce faster applications.

The language, according to the authors, was influenced by C #, Objective-C, Haskell, among other things (see syntax description below). Perhaps with a retroscope. However, it is based on LLVM tools and produces native code. This means a very old syntax with a modern backend.

Tom Swift

Comparison with the Go language naturally comes to mind, as Swift wants to be the successor to Objective-C, while Go wanted to do the same with C++. Swift contains several Go syntax elements:

However, it differs from Go on many important issues, including the use of inheritance when Go uses composition. Competition is a library for Swift, the generality lacks Go. Go is a minimalistic language (one loop syntax), while Swift seeks to integrate all possible elements into every aspect of the language.

Swift was originally the owner, but on December 3, 2015, Apple published the source code (compiler + library). Creating a program requires XCode. It is presented from the very beginning with advanced documentation, a complete tutorial and a TypeScript player for code testing. In fact, its design began in 2010, it took four years to prepare for its public distribution.

Language features...

Syntax description

Show the world hello!

print("Salut le Monde!") 

You can insert variables with the\() operator, which, on the contrary, $ from PHP has the advantage of separating the end of the variable name .

Output defined type

As in Go, Julia and many screenwriting languages.

var x = 10

The assigned integer value gives x type int. The type cannot change as it would with dynamic language.

Explicit type

This has become common, variables are defined by var and type can be specified as a variant, but variables are not dynamic as in Julia.

var s = "hello" : string

Constant

Constants are determined by let. There is no apparent advantage over const and the word let over another role in other languages such as JavaScript or in BASIC, which originally used the word for assignment in general .

let x = 10

Train

The tuple may be automatically decomposed and thus provide multiple returns from corrupted functions or assignments.

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

x is 1000 and y is hello.

Control structure if

Repeats the Go principle: not brackets, but mandatory brackets:

if x == 10 {  
    print("égal à 10")
} else if x < 10 {
    print("moins que 10")
} else {
    print("plus de 10")
}

Loop for

"One interval" looks like Ruby and Scriptol. Swift uses.. <for exclusive interval,... for the inclusive interval. Ruby uses... for exclusive interval.
Double points are used in the print shop to represent the interval, which was also taken by Bash, while the three hardpoints represent an indeterminate continuation.
Python, for his part, repeats the symbolism of ALGOL 68.

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

To select the increment, the C format was also implemented. Rather, one could add such a step variant as in BASIC. It was removed in Swift 3.

For in works on a collection that is now implemented in all languages, even C++.

for x in liste {
    print("Item \(x)")
} 

The classic example for displaying string letters is as simple in Swift as in Python.

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

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

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

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

While and rehearsal while: Retro-fashion language BASIC

For some reason, the loop to... while version 1 inherited from C (1972) is replaced by repetition... while, reminiscent of the until rehearsal of the BASIC language (1964).

while x <  y {

}
repeat {

} while x < y

Switchboard

It solves the question of multiple cases by separating them with commas. Like Go, it can also use the word fallthrough for sentence chains. Intervals are possible as in Script.
You can also use tuples in boxes, but compare them with the original tuple.

switch x {
case 1:
    print("1")
case 2, 3, 4:
    print("entre 2 et 4")
case 5 ... 10:
    print("entre 5 et 10")
default :
    print("autre valeur")
}

The break and continue clauses associated with a label in a switch inserted in a while loop allow you to either jump to the statement or jump to the next iteration.

Pattern match

Check boxes in the switch structure can contain where conditions. The principle dates back to the SNOBOL language (1962). Python added a matching pattern extension called SnoPy.

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

Function

The syntax is reversed from C, as is fashionable with new languages. It supports multiple returns, default settings. You can nest functions in another function that are not available from it.

func mafonction(s : String) -> String {
   s += "suffixe"
   return s
}

Multiple returns as a tuple. The variable name must be specified in both the header and the return statement.

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

Variadic parameters with the symbol of three dots, inspired by C, symbolism, 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 a table, but this is not visible when the function is called.

Classes and inheritance

Classes with simple inheritance and multiple interfaces, such as Java, C #. The constructor is called init rather than constructor, or rather repeats the name than the class, perhaps because it is more general .

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

class Car : Vehicle {
   var passengers : Int
}

Protocol

The interface is called a protocol to be original. They can be typical or generic. The principle is inspired by C # with getters and setters: the words get and set clarify whether a property can be read or assigned.

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

The interface (protocol) is inherited as a class:

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

Expansions

Traits here are called extensions. This allows you to add methods to existing classes. Here you can see the influence of Rust: changing the name of familiar elements to languages ​ ​ gives them a kind of novelty!

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

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

Who will use Swift?

The adoption of Swift should not cause problems for Apple mobile developers. They already use a special language on the platform, Objective C, and migrating to a more modern and compatible version can only make life easier for them.
Possible production of LLVM bitcode can only give access to Android if the application does not use the Apple framework and XCode is no longer needed. The same limitation applies to other operating systems.
The authors also develop a language for server-side use, and several frameworks exist for this. Useful components are implemented in the Open Swift project. But in 2017, Swift is not ready for non-departmental use and since each new version adds incompatibility with the previous one, it is better not to use it until it is completely stable. Moreover, his documentation is also not relevant. In addition, it is seriously suitable only for XCode, therefore only for Apple.

Site:

Programming and dated languages - Asm.js- - BASIC- C- -C++ # C- - Darth-Eiffel - Guo- --JavaJavaScript--Julia PascalPHP- - - Python- - Prolog RubyLa ScalaScriptolSwift TypeScriptHTMLVasmXMLXAML SQL