#MonthOfJulia Day 3: Variables and Data Types

Most coding involves the assignment and manipulation of variables. Julia is dynamically typed, which means that you don’t need to declare explicitly a variable’s data type. It also means that a single variable name can be associated with different data types at various times. Julia has a sophisticated, yet extremely flexible, system for dealing with data types. covered in great detail by the official documentation. My notes below simply highlight some salient points I uncovered while digging around.

One of the major drawbacks of dynamically typed languages is that they generally sacrifice performance for convenience. This does not apply with Julia, as explained in the quote below.

The landscape of computing has changed dramatically over the years. Modern scientific computing environments such as MATLAB, R, Mathematica, Octave, Python (with NumPy), and SciLab have grown in popularity and fall under the general category known as dynamic languages or dynamically typed languages. In these programming languages, programmers write simple, high-level code without any mention of types like int, float or double that pervade statically typed languages such as C and Fortran. Julia is dynamically typed, but it is different as it approaches statically typed performance. New users can begin working with Julia as they did in the traditional numerical computing languages, and work their way up when ready. In Julia, types are implied by the computation itself together with input values. As a result, Julia programs are often completely generic and compute with data of different input types without modification—a feature known as “polymorphism.” Julia: A Fresh Approach to Numerical Computing

Variables and Assignment

Variable assignment and evaluation work in precisely the way you’d expect.

Julia supports Unicode, so ß, Ɛ and Ȁ are perfectly legitimate (although possibly not too sensible) variable names.

Chained and simultaneous assignments are possible.

Multiple expressions can be combined into a single compound expression. Julia supports both verbose and compact syntax.

Constants, declared as const, are immutable variables (forgive the horrendous contradiction, but I trust that you know what I mean!).

Somewhat disconcertingly it is possible to change the value of a constant. You just can’t change its type. This restriction has more to do with performance than anything else.

There are numerous predefined constants too.

Data Types

The practical implications of picking number types are pretty important. Efficient programming requires you to use the least amount of memory. Julia in Action by Chris von Csefalvay

Julia has an extensive type hierachy with its root at the universal Any type. You can query the current data type for a variable using typeof(). As mentioned above, this is dynamic and a variable can readily be reassigned a value with a different type.

There are various other functions for probing the type hierarchy. For example, you can use isa() to check whether a variable or constant is of a particular type.

So we see that 8 is both a In64 and a Number. Not only does that make mathematical sense, it also suggests that there is a relationship between the In64 and Number data types. In fact Int64 is a subtype of Signed, which derives from Integer, which is a subtype of Real, which derives from Number

Formally this can be written as

where <: is the “derived from” operator. We can explore the hierarchy in the opposite direction too, where subtypes() descends one level in the type hierarchy.

Numeric Types

Julia supports integers between Int8 and Int128, with Int32 or Int64 being the default depending on the hardware and operating system. A “U” prefix indicates unsigned variants, like UInt64. Arbitrary precision integers are supported via the BigInt type.

Floating point numbers are stored by Float16, Float32 and Float64 types. Arbitrary precision floats are supported via the BigFloat type. Single and double precision floating point constants are given with specific syntax.

In Julia complex and rational numbers are parametric types, for example Complex{Float32} and Rational{Int64}. More information on complex and rational numbers in Julia can be found in the documentation.

An encyclopaedic selection of mathematical operations is supported on numeric types.

Characters and Strings

Julia distinguishes between strings and characters. Strings are enclosed in double quotes, while individual characters are designated by single quotes. Strings are immutable and can be either ASCII (type ASCIIString) or unicode (type UTF8String). The indexing operator [] is used to extract slices from within strings. Evaluating variables within a string is done with a syntax which will be familiar to most shell warriors.

There is a lot of functionality attached to the String class. To get an idea of the range, have a look at the output from methodswith(String).

Julia supports Perl regular expressions. Regular expression objects are of type Regex and defined by a string preceded by the character ‘r’ and possibly followed by a modifier character (‘i’, ‘s’, ‘m’ or ‘x’).

Explicit type conversion works either by using convert() or the lower-case type name as a function.

Type Specifications

Although Julia is dynamically typed, it’s still possible (and often desireable) to stipulate the type for a particular variable. Furthermore, from a performance perspective it’s beneficial that a variable retains a particular type once it has been assigned. This is known as type stability. We’ll find out more about these issues when we look at defining functions in Julia.

As before, my detailed notes on today’s foray into Julia can be found on github.