BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Apple Releases Swift, a High-performance High-level Language for iOS and OSX

Apple Releases Swift, a High-performance High-level Language for iOS and OSX

Leia em Português

This item in japanese

Lire ce contenu en français

Today at WWDC 2014, Apple announced the beta availability of a new programming language, Swift, which is set to ship with iOS 8 and OSX Yosemite later this year. Swift is a high-level programming language that will be familiar to JavaScript developers, but is compiled using LLVM to produce highly performant executable code for both OSX and iOS platforms.

Apple has heavily invested in LLVM technologies, which provide an abstract instruction set that can be translated for specific architectures. Clang replaced GCC as the compiler of choice for C and Objective-C programs, and both of those are translated with Clang into LLVM instructions, which are then optimised into an executable for the platform. The new programming language, Swift, produces LLVM bytecode in the same way and can co-exist with existing Objective-C applications and libraries.

In addition, swift also comes with a REPL environment for testing code. Normally used by interpreted languages such as JavaScript or Python, a REPL provides a Read-Evaluate-Print Loop that can be used to evaluate individual expressions and statements at the command line for easy debugging. Combined with powerful looping, string interpolation and printing/debugging options, it allows an interactive style of development and testing that is frequently missing for compiled languages such as C and Java.

Swift is a strongly typed language, though it uses type inference to reduce the amount of characters typed by the developer. Types can be provided explicitly, and for numeric types, specific sized elements such as UInt8 or Int32 can be used.The type engine also provides for Optional valued types; an optional value can be dereferenced with a ? or ! depending on whether the value is expected to exist. The collections in Swift provide literal support for both arrays (with []) and dictionaries/maps (with [:]) and can include generic types.

Functions are also first class elements in the language, and can be used to pass functions as variables to other elements. Arguments can also be named explicitly (which provides compatibility with Objective-C’s named arguments) or provided as positional arguments only. Parameters can also have default values, allowing arguments to be omitted if not required.

The swift integration can use Objective-C objects, both from the standard libraries (such as UIKit or Cocoa) as well as user-defined types. Classes can also be created in swift natively (which can then also be used from Objective-C) which, like other Objective-C objects, are reference counted. (The references are managed automatically by the runtime, so the user does not need to worry about memory management in swift directly.) As well as classes, which are reference types, it is possible to create struct value types that can be passed by copy as values to function types.

Finally swift has multiple return types, through the use of tuples. These tuples can be used in matching semantics (with the case statement) or variable assignment.

Swift is available via the Xcode 6 developer preview, and will be released in production in the next few months. At this time, there is no confirmation that the swift language will be merged upstream with LLVM, but having recently merged the ARM 64-bit support and worked on the fast JIT for Safari’s JavaScript, it is likely that some of it will fold into the upstream clang runtime hosted by LLVM. This could provide the ability for swift programs to be written for other architectures where there is a clang compiler.

Rate this Article

Adoption
Style

BT