Editing
Struct
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Struct == A **struct** is a composite data type that allows developers to define their own data structures with named fields. It is a fundamental concept in the Elixir programming language. === Purpose === Structs are useful for organizing and manipulating related data in a structured manner. They provide a convenient way to define and work with complex data shapes in Elixir. By defining a struct, you can ensure that the data adheres to a specific structure with predefined fields. This makes it easier to reason about the data and perform operations on it. === Syntax === To define a struct in Elixir, you use the `defstruct` keyword followed by a list of field names: ```elixir defmodule MyStruct do defstruct [:field1, :field2] end ``` Here, `MyStruct` is the name of the struct, and `:field1` and `:field2` are the names of the fields. To create an instance of the struct, you can use the `new/1` function: ```elixir my_struct = %MyStruct{} ``` You can also initialize the fields with default values: ```elixir my_struct = %MyStruct{field1: value1, field2: value2} ``` === Accessing and Updating Fields === You can access the fields of a struct using the dot notation: ```elixir my_struct.field1 ``` To update the value of a field, you use the `put_in/2` function: ```elixir new_struct = put_in(my_struct.field1, new_value) ``` === Pattern Matching === Structs can be used in pattern matching to destructure and extract values. This is particularly useful when working with collections of structs or when filtering data based on specific field values. ```elixir %MyStruct{field1: value} = my_struct ``` In this example, the variable `value` will be bound to the value of `field1` in `my_struct`. === Implementing Behaviours === Structs can also be used to implement behaviours in Elixir. By defining a struct that implements a specific set of fields and functions, you can ensure that certain behaviours are adhered to by any data structure that uses that struct. === Conclusion === Structs are a powerful tool in Elixir for organizing and manipulating data. They allow developers to define their own custom data structures with named fields. Structs provide a convenient way to work with complex data shapes and ensure data consistency. Whether used for organizing collections of data or implementing behaviours, structs are an essential part of the Elixir language. == See Also == * [[Pattern Matching in Elixir]] * [[Behaviours in Elixir]]
Summary:
Please note that all contributions to Elixir Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Elixir Wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information