In Elixir, Structs are maps with reduced functionality, compile-time checks, and default values.
Reduced functionality means that structs cannot use protocols defined for maps like Enum
, but can use functions from the Map
module.
Compile-time checks ensure that structs do not have any variable that has not been declared for it, and that default values are assigned values to struct member variables if no value has been explicitly assigned.
Structs need to be defined before use:
defmodule City dodefstruct name: "New York City", state: "New York", country: "USA"end
In this example, a City
struct is defined. The struct name follows the defmodule
keyword, and the variables and their default values follow the defstruct
keyword.
If no default value is assigned to a variable when the struct is defined, and if that variable is not given a value when a struct object is defined, then that variable is given the nil
value. All variables without default values need to be declared to the left of variables with default values.
defmodule City dodefstruct [:name , :state, country: "USA"]enddefmodule City2 dodefstruct name: nil , state: nil, country: "USA"end
As can be seen above, any variable name without a default value is written like an atomic value, but all variables are in square brackets. Another way, without square brackets, would be to explicitly give these variables a default value of nil
. If any of these variables without a default value are moved to the right of country
, the program will give an error:
# Defining structdefmodule City dodefstruct name: "New York City", state: "New York", country: "USA"end# Defining struct with some undefined property valuesdefmodule City2 dodefstruct [:name , :state, country: "USA"]enddefmodule Main dodef main do# Defining struct variables of City structlondon = %City{name: "London", state: "Greater London", country: "UK"}rochester = %City{name: "Rochester"}newyork = %City{}# Displaying variablesIO.puts "Displaying struct variable:"IO.inspect londonIO.puts "Displaying struct variable with a few default values:"IO.inspect rochesterIO.puts "Displaying struct variable with all default values:"IO.inspect newyork# Defining struct variable of City2 structlosangeles = %City2{}# Displaying variableIO.puts "Displaying struct variable with all default values and some without definition:"IO.inspect losangeles# Updating valuesIO.puts "Changing value of struct properties:"losangeles = %{losangeles | name: "Los Angeles"}losangeles = %{losangeles | state: "California"}IO.inspect losangeles# Accessing individual struct variableIO.puts "Accessing individual struct property:"IO.puts losangeles.nameendendMain.main
This example demonstrates how structs are declared, accessed, and changed, and how they behave depending on the presence and absence of default values.
As seen in the example, the syntax to change the value of a struct property is as follows: objectStorageVariable = %{objectToBeChanged | property: newValue}
. Here, objectStorageVariable
is the variable in which the object with the changed value is to be stored. objectToBeChanged
is the object variable that is going to be changed. property
is the struct property whose value to be changed. newValue
is the new value to be assigned to the property. To access a single struct property, the object.property
syntax is followed.
Free Resources