Saturday, May 28, 2016

Swift for the Java guy: Part 1 - Getting Started

It's been a while since I had to look at the Apple development Eco-system.

In fact the last time I did anything serious was back in 2009 when I tried to get in on the boom in apps at the time.

I honestly didn't have a lot of issues with Objective-C as a language per se. In fact if anything I much preferred that than if I had had to whack out code in C or C++.

I think my main objection - if I had one - was that the Xcode environment is nowhere close to the level of something like IntelliJ. Also the SDKs at the time were pretty limited compared to what you had on the full blown version of OSX.

Needless to say; I spent a lot of time doing a lot more boilerplate code than what I would have liked.

When Swift came out (almost two years ago now, can you believe it) I briefly looked at the language which I thought was quite nice but it didn't really interest me much. The biggest reason for this is that my world has been - for the most part - Server side Java running on some type *nix based platform.

Fast forward two years and suddenly Swift is OSS and companies like IBM are trying hard to make Swift feature in my world.

Ok but what can it do that Java cannot?

Well here's a few things to consider:

  • Docker is changing the landscape in terms of how we deploy software. In a lot of ways Docker is the new Application server and the new virtual machine.
  • Together with Docker we have the rise of Microservices and the need to create very small deployable units of code.
  • Swift is a native language, it's fast out the box and incurs very little overhead in terms of memory and startup time.
  • Swift is also a thoroughly modern and clean language.
  • The combination of these things potentially makes Swift a great platform to build Dockerised Microservices, perhaps better than a language which historically incurs a large cost with it's VM and dependent libraries.


Installing.

If you are you are using OSX the easiest way to get running is simply to get the latest version of Xcode from the App Store.

If you are running Ubuntu (14.04 or 15.04) I suggest you use the excellent swiftenv utility to install and manage Swift versions.

You can also install swiftenv for OSX via Homebrew. This is useful if you want to easily play around with the Swift 3.0 development builds.

The system I am using for this post is running Ubuntu 14.04 and I using the 2016-05-09-a version which is a Swift 3.0 development snapshot. 

Firstly I installed swiftenv as follows:

git clone https://github.com/kylef/swiftenv.git ~/.swiftenv
echo 'export SWIFTENV_ROOT="$HOME/.swiftenv"' >> ~/.bashrc
echo 'export PATH="$SWIFTENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(swiftenv init -)"' >> ~/.bashrc

Then I can go ahead and install the Swift development package.

swiftenv install DEVELOPMENT-SNAPSHOT-2016-05-09-a

That's it! Swift is now installed.

You can test the installation by running the Swift REPL

Building your first bit of Swift.

My first issue that I encountered on Linux is the complete lack of a decent IDE for the Linux environment. For this example I ended up using good old Vim. 

However I'm - somewhat ironically - playing around with Visual Studio Code which also seems to do a decent job.

For this example I will do the obligatory "Hello world"

I simply create a file "HelloWorld.swift" and I put the following code in it.

print("Hello world")

And that's it. Swift will basically run any code which is declared inline alla Ruby, Python, Groovy et-al.

You can now run this as follows:

swift HelloWorld.swift

at which point you see the output:

hello world

Wait a minute didn't I say this is compiled!?

Another really neat feature of Swift is that you can run code both in interpreted and compiled mode. 

To compile the code you simply run:

swiftc HelloWorld.swift

This spits out the Executable "HelloWorld" which you can simply execute as:

./HelloWorld

And there you have it! Swift is up and running. In the next post we will explore the language a bit.

Some Notes

swiftc didn't work the first time on my machine. It turns out that Swift relies on a version 3.6 of Clang on Ubuntu 14.04

I fixed this by doing the following:

apt-get install clang-3.6
update-alternatives /usr/bin/clang clang /usr/bin/clang-3.6 100
update-alternatives /usr/bin/clang++ clang++ /usr/bin/clang++-3.6 100





No comments: