Author: Rheinard Korf

Building Swift from Source

All my Swift learning thus far has been using the binaries provided from the site.  This is good most of the time and it is what I’ve included in my Vagrant project, Vagrant Swiftbox.






I hope this helps someone.

Get better at Swift by learning Haskell

A few months ago someone was desperately trying to get me to learn Haskell. I really wanted to, but kept putting it off. So yesterday I installed ghc a Haskell compiler and started playing. I should have done this ages ago!

So after that intro, its been a few weeks since my last post. Various things have happened, including me developing the dreaded coder’s pinky (painful wrists). So for a while there I was a bit worried and started teaching my computer how to code with my voice. Its doable, but for now simply replacing IDE shortcuts with voice commands, this is already a huge win… ok, back to Haskell.

As I’ve been learning Swift and going through the language I started seeing a lot of things that did not make sense to me, for one, I learned to code with variables at a young age. Why on earth is all Swift documentation trying to get me to use immutable objects? It did not make sense. Then I started seeing some more code that I didn’t understand… Closures in Swift looked so foreign to me. I’m used to anonymous functions in JavaScript, PHP 5.3+ and lambdas in Ruby, but Closures in Swift really had me totally confused.

Enter Haskell. I started noticing notation in Swift that I’ve seen before in a programming Slack channel. It looked similar to Haskell and I got curious. Turns out that Swift was very much inspired by Haskell and other languages. Thats all I needed to know to finally give Haskell a try.

Paradigm Shift

I started playing with Haskell and my mind was blown. Functional programming is very different to my OOP thinking. Favouring struct over class, everything immutablelist comprehensioninfinite lists… this stuff is crazy. But as you play around with it things slowly start to make sense and you can’t help but wanting to learn more.

Back to Swift

After playing only a little bit with Haskell and looking at Swift code again, things are making a whole lot more sense. I realised that I can write functions in Swift without even using func structures, operator overloading, filter, map, reduce, but also writing my own generic functions (not so comfortable with that yet, but getting there).

Functional programming is not the be all end all (IMHO), but it sure helps a whole lot if you’re wanting a deeper and better understanding of what is possible with Swift (and I’m not talking iOS or OSX dev here, I’m talking about the language itself).

If you’re learning Swift or already quite comfortable with it, do yourself a favour, head over to “Learn You a Haskell”. Its a nice guide to help you learn Haskell (despite the comical name).

Happy coding!

Swift Scripting… things to know.













// Get Foundation (for what its worth)
import Foundation

let isLinux: Bool
let isOSX: Bool

#if os(Linux)
  // Get Glibc 
  import Glibc
  isLinux = true
  isOSX = false
#elseif os(OSX)
  // Get Darwin
  import Darwin
  isLinux = false
  isOSX = true
  isLinux = false
  isOSX = false




Swift Grammar: Its a language thing.

Usually when I learn a new language I tend to jump straight into the “Hello World” example and get my hands wet immediately. Because I have already used the first iteration of Swift and more recently in a project I used Swift 2, I’m going to do this deep dive into the language a little bit differently.

Rather than starting with examples, I’m going to start with the language grammar. Traditionally this is not how people are taught a language, but I find that looking at the grammar of a language gives you a much better understanding of how the language works.

I’m going to work from the grammar as it is defined in the language reference. See, “About the Language Reference” under the title “How to Read the Grammar” for an explanation of the syntax.

First things first, the grammar is made up of tokens. A token consists of the following: An identifier, a keyword, punctuation, a literal or an operator. This can seem very confusing if you have never looked at syntax before, so lets break down a simple statement.

var number = 2

This is a valid token in Swift. var is a keyword, number is our identifier, = is an assignment operator and 2 is a numerical decimal literal. We know that this token is valid because the Swift grammar tells us that it is. In this case the grammar is:

statement → declaration ;optional

declaration → variable-declaration

variable-declarationvariable-declaration-head pattern-initializer-list

variable-declaration-head → attributesoptional declaration-modifiersoptional var

pattern-initializer-list → pattern-initializer | pattern-initializer, pattern-initializer-list

pattern-initializer → pattern initializeroptional

pattern → identifier-pattern type-annotationoptional

identifier-pattern → identifier

identifier → identifier-head identifier-charactersoptional

identifier-head → Upper- or lowercase letter A through Z

identifier-characters → identifier-character identifier-charactersoptional

identifier-character → identifier-head

initializer= expression

expression → try-operatoroptional prefix-expression binary-expressionsoptional

prefix-expression → prefix-operatoroptional postfix-expression

postfix-expression → primary-expression

primary-expression → literal-expression

literal-expression → literal

literal → numeric-literal | string-literal | boolean-literal | nil-literal

numeric-literal → optional integer-literal | optional floating-point-literal

integer-literal → decimal-literal

decimal-literal → decimal-digit decimal-literal-charactersoptional

decimal-digit → Digit 0 through 9

That is an amazing amount of steps that Swift need to evaluate to determine whether such a simple variable declaration is valid grammar or not. As I was going through the grammar I chose the shortest possible path and I’m glad I only chose one digit. I did not even touch on the full grammar for each of the tokens.

As you are learning Swift I’d encourage you to take a look at the grammar of even your simplest statements to get a more in depth understanding of the language.

I personally find this fascinating to go through code like this, but I’m sure that this post is not for everyone. Hang in there, we’ll get to code soon, but from time to time I might refer back to the grammar.

Enjoy digging into the grammar of Swift.

Geek aside

The manual uses a simple way to represent the grammar of Swift using Bachus-Naur Form (BNF). Some might argue this because of the use of the → symbol over a more accepted ::= sequence of characters, but in the end, its just a representation of how the syntax for the language is structured. Read up on BNF to get a better understanding of language grammars and as an added bonus in your learning, do some reading on finite-state machines (FSM), which could be used as a more visual way to represent a language grammar.

Beginning Swiftly

So Friday turned out to be quite an exciting day!  PHP 7 released, Lets Encrypt went public beta, but the one I want to focus on, Swift officially became open source. I wasn’t too surprised because even at the first release there were rumours and already published its vision (which was obvious reading between the lines).

Nevertheless, this is exciting news and I decided that going all in on this means getting your hands on the latest version, which means if you’re on a OSX installing the latest Xcode beta. I was not about to do that and since Swift is available on linux I decided to build a Swift vagrant file. This means that you can easily launch virtual machines that you can start coding Swift 2.2 right away.

If you’d like a copy grab it over here, vagrant-swiftbox, and then head over to – Getting Started.

So for the next while as I dive into Swift outside of its normal Xcode habitat I will be documenting my learning here for myself to remember, but also for you to explore.

Happy learning!