Julia Community 🟣

Cover image for Structs in Julia
Ifihanagbara Olusheye
Ifihanagbara Olusheye

Posted on • Originally published at hashnode.ifihan.dev

Structs in Julia

Asides from the several built-in data types Julia offers like string, integer, etc., Julia gives a programmer the ability to create their own data type using a type called struct. This helps us organize data to our specific needs.

What is a struct in Julia?

A struct in Julia is a composite data type that allows you to store multiple values in a single object. Structs are similar to classes in object-oriented programming languages but are simpler and more lightweight. Structs are useful for organizing related data into a single object, and for creating data types with custom behavior.

Structs are immutable by default; after being created, an instance of one of these types cannot be changed. To declare a type whose instances can be changed, the mutable struct keyword can be used instead.

How to define a struct in Julia

To define a struct, the struct keyword is used. Here is the basic syntax for defining a struct:

struct MyStruct
    field1::Type1
    field2::Type2
end
Enter fullscreen mode Exit fullscreen mode

Where MyStruct is the name of the struct and field1 and field2 are the names of the fields of the struct. Type1 and Type2 are the types of the corresponding fields.

For example, let's define a simple struct Person with three fields name, age, and phone_number.

struct Person
    name::String
    age::Int
    phone_number::Int64
end
Enter fullscreen mode Exit fullscreen mode

This defines a Person struct with three fields: name of type String, age of type Int, and phone_number of type Int64.

You can create a new instance of the Person struct as follows:

p = Person("John Doe", 30, 1234567890)
Enter fullscreen mode Exit fullscreen mode

This creates a new Person object with the name "John Doe" of age 30 and a phone number of 1234567890.

The values of the fields can be accessed using the dot notation, like this:

println(p.name)
println(p.age)
println(p.phone_number)
Enter fullscreen mode Exit fullscreen mode

The following code above returns the value of the fields, “John Doe”, 30, and 1234567890 respectively.

Why use Structs in Julia?

Structs are a great way to organize complex data in Julia. They allow you to store related data in a single object and provide custom behavior for your data types. Structs are also lightweight and more efficient than classes, making them faster and easier to use. Finally, structs are a great way to make your code easier to read and understand, as they make it clear which data belongs to which object.

Conclusion

Structs in Julia are a powerful tool for creating custom data types that can help you organize and manipulate data in a way that makes sense for your specific use case. Whether you are working on a small personal project or a large-scale application, structs can help you get the job done quickly and efficiently.

I would be happy to hear your thoughts on this article. Feel free to reach out to me on Twitter, LinkedIn, or by Email.

Top comments (2)

Collapse
 
ohanian profile image
Steven Siew • Edited

Don't forget to talk about github.com/JuliaObjects/Accessors.jl

It let you change the values of the fields easily.

Check out the example here: juliaobjects.github.io/Accessors.j...

Collapse
 
ifihan profile image
Ifihanagbara Olusheye

I just checked it out. This looks really good. Thank you!