Swift Kick: Variables, constants, simple functions, and basic types in the Swift programming language

by Joey deVilla on June 4, 2014

swift kickLet’s dive into Swift! For the next several articles, I’m going to share my observations as I noodle around with Swift on the Xcode 6 beta and try to get a feel for the language.

Keep in mind that Swift is in beta, and given the loose way “beta” is defined in these agile, pivot-happy days, it could be subject to all sorts of changes that may invalidate some of what I cover here.

Example 1: No semicolons, implicitly-typed variables, basic variable types, output, and string interpolation

We’ll start off with a couple of variable declarations, and then we’ll print them out on the console. Here’s the code:

And here’s its output:

No semicolons

Note that unlike Objective-C (or C, or most other languages that borrow its syntax), the lines of code above don’t end in semicolons. Swift is like JavaScript in that a line break is enough to mark the end of a statement, and ending them with semicolons is optional (much to the chagrin of the pro-mandatory-semicolon people in the Great JavaScript Semicolon Debate). You do need to use a semicolon to separate statements if you want to put them on the same line. For example, the following code compiles:

My recommendation: follow the style in Apple’s example code, be like our friends in the Lua, Python, and Ruby worlds, and don’t use semicolons to separate statements.

Implicitly-typed variables

Note that we didn’t have to specify the types of the name and radius variables. All we did was declare them with the var keyword, and then we assigned values to them:

This code is legal JavaScript, but it’s rather different underneath. JavaScript associates types with values, while Swift associates types with variables. The compiler infers the variables’ types from the values assigned to them:

  • name was assigned a string literal, which means that it should be of type String, and
  • radius was assigned a numeric literal with a decimal point, so the compiler assigned it the type Double.

This feature goes by names like type inference and implicit typing. We’ve had this in C# since late 2007, and it’s nice to see it in Swift.

Swift’s basic types

The basic types that I’ve encountered in the documentation so far are:

  • Int
  • Float
  • Double
  • Bool (whose values are true and false as opposed to Objective-C’s YES and NO)
  • String

Output

The println() function sends output to the console. It’s capable of outputting all of Swift’s basic types without your having to convert them to String first. All of the following lines work in Swift:

String interpolation

\() is used for string interpolation, in a manner similar to Ruby’s and CoffeeScript’s #{}.

Example 2: Defining functions, defining constants, and Unicode names

Let’s take our initial code and add a function that calculates the area of a circle, given its radius:

Here’s its output:

Defining functions

Function headers in Swift have the following format… function headers in swift …and they’re bounded by braces — { and } — just like in Objective-C and every other C-like language. The return keyword works as you’d expect it to: it specifies the function’s return value and returns program control to whatever called the function in the first place.

There’s a lot more to function definitions, and I’ll cover than in a later article.

Defining constants

Take a closer look at the definition inside the circleArea function:

Just as var is used to declare variables, let is used to declare constants. As with variables, if there’s enough information in the value you’re assigning to the constant, the compiler will infer its type.

As you might expect, any attempt to redefine a constant results in an error. After all, a redefinable constant is a variable.

Unicode names

In our constant declaration, we used the greek letter π (which you get by typing option-p) instead of pi.Swift’s source files are Unicode, which means that you’re not limited to the Roman character set for naming things. This feature is meant to let people whose native languages with non-Roman alphabets give more meaningful names to their variables, constants, functions, classes, and so on.

As I wrote in an earlier post, it also lets you use emoji, which may be useful for amusing demos or in the event someone declares an Obfuscated Swift programming contest.

Explicitly declaring variable types

There are times when the compiler just doesn’t have enough information to infer the type of a variable. For instance, if you make the following declaration…

…the compiler will assume that someValue‘s type is Double. What if you need someValue to be a Float instead? You’ll need to specify its type, as shown below:

If you need to declare a variable but don’t want to assign a value to it immediately, you’ll also have to specify its type, as the compiler won’t have enough information to infer it:

Example 3: Remember, Swift is a strongly-typed language

Try the following code, but take note: it won’t work!

The problem is that you’re passing a Float to a function that expects a Double parameter. You need to convert floatRadius into a Double first. Luckily that’s pretty simple — all the basic types have conversion functions:

  • Int() takes a numeric or Bool argument and returns its Int equivalent, with the fraction removed (not rounded). The Bool value true is converted to 1, false is converted to 0.
  • Float() takes a numeric argument and returns its Float equivalent. You lose decimal precision if you use Float() on a Double, and gain some if you use it on an Int. The Bool value true is converted to 1.0, false is converted to 0.0.
  • Double() takes a numeric argument and returns its Double equivalent. You gain decimal precision if you use Double() on an Int or Float. The Bool value true is converted to 1.0, false is converted to 0.0.
  • Bool() takes a numeric or String argument and returns its Bool equivalent. As far as I can tell, only 0 equates to false; everything else — even the empty string "" — equates to true. I tried Bool(nil) and got an error.
  • String() takes a numeric argument and returns its String equivalent.

This code works, because we use Float() to convert floatRadius into a Double before passing it to circleArea():

Why all the type fussiness?

swift speed

Craig Federighi talks about Swift’s swiftness.

The payoff for all this fussiness about type is speed. As Matt Galloway writes in his Swift Language Highlights article on RayWenderlich.com:

In Swift, the compiler knows much more [than Objective-C] about the types in play in any method call. It knows exactly where [any given function or method] is defined. Because of this, it can optimise certain call sites by jumping directly to the implementation rather than having to go through dynamic dispatch. In other cases, it can use vtable style dispatch, which is far less overhead than dynamic dispatch in Objective-C. This is the kind of dispatch that C++ uses for virtual functions.

Leave a Comment

Previous post:

Next post: