Introduction to Types in Julia

Julia’s type system is one of its core strengths and a key to the language’s high performance. It also enables a flexible programming style such that the programmer can often ignore types entirely or choose to explicitly specify them only when it’s useful for expressivity or to give the compiler hints that can improve performance. When left unspecified, Julia can often discover types at compile time by examining your code, or at runtime (during the execution of your program) by tagging values as they move throughout the program. When Julia knows the types of values at compile time, it’s able to generate faster code that’s optimized for those specific types.

But what exactly is a type? In programming languages, types specify facts that are true for any values (or instances) of that type. For example, Alice and Bob might both be represented by the Person type, and all instances of the Person type have name, age, and birthday fields and support the run method. So, if the compiler knows that a given value (say Alice) is of the type Person, it knows it can access those fields without checking if they exist. It might even know where in memory that data is located.

Types are also an expressive tool for programmers to categorize objects that behave differently. For instance, the Person type above supports the run method that moves them at a certain speed, whereas values of the Cheetah type might run faster.

Note

Languages are often categorized as dynamically ...

Get Learning Julia: Introduction to Types now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.