What about Swift?

We are seeing a resurgence in programming language design. Watch out C, Java, a change may be in the wind.

Swift seems like it has overcome some of the inadequacies of C-like languages. Swift is an elegant language, and has enhanced readability due to a much simplified syntax. Why is it more readable?

  1. Semicolons be gone! – they aren’t needed unless there are multiple statements on the same line.
  2. Forced use of block statements – C-like languages allow either a single statement, or a block enclosed in { } after a control structure. Swift does not. Apart from switch statements, all other control structures require braces and no single line statements are allowed. Vamoose dangling else!
  3. Swift does not allow the use of the assignment (=) operator in if statements, where there should have been a == (unlike C and Objective-C).

Swift also does neat things with for loops, by way of a for-in loop, and ranges.

for i in 1...5 {
    println(i)
}

Here is an example for calculating a Fibonacci number:

var fibN0 = 1
var fibN1 = 1
for i in 3...50
{
    var sum = fibN0 + fibN1
    fibN1 = fibN0
    fibN0 = sum
}

The weird thing about Swift may be its variables. Not what you would normally expect. In Swift you can create variables or constants, and they are implicitly typed (but the type can also be specified). Swift also allows the use of tuples, which means groups of values can be treated as a single unit.

And yippy! the goto statement has been banished.

The downside? Console input seems to be *lacking*. I get that some of these languages aren’t designed for STDIN, but the language should provide an easy way of doing it. I mean they do provide print and println for output?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s