Ad space (banner)
๐ŸŸฃJulia Lessons
Lesson 9 / 68

Working with structs

This lesson covers gathering data with Julia's struct, with the aim of handling several related values as one thing.

struct defines a structure. Julia has no classes, but a struct gathers related data together, and combined with functions it gives you something close to object-oriented design.

The sample code defines struct Animal with two typed fields, name::String and sound::String, and assembles the sound message in a speak(a::Animal) function. Note the :: notation that types a function argument.

A common early stumble is that Julia's object orientation is not class-based. Separating data (struct) from behaviour (functions), in the multiple-dispatch style, takes some getting used to.

In professional work, structs are used widely wherever related data belongs together — coordinates, experimental results.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Julia directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)