# What are the uses of some of the numerous "types" in Julia e.g., structs, modules?

**URL:** https://discourse.julialang.org/t/what-are-the-uses-of-some-of-the-numerous-types-in-julia-e-g-structs-modules/100708
**Category:** New to Julia
**Tags:** question, type
**Created:** [June 22, 2023, 7:02pm UTC](https://discourse.julialang.org/t/what-are-the-uses-of-some-of-the-numerous-types-in-julia-e-g-structs-modules/100708 "2023-06-22T19:02:47Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![goerz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerz/32/3269_2.png) [@goerz](https://discourse.julialang.org/u/goerz)
#### Post date: [June 22, 2023, 7:58pm UTC](https://discourse.julialang.org/t/what-are-the-uses-of-some-of-the-numerous-types-in-julia-e-g-structs-modules/100708/2 "2023-06-22T19:58:33Z")

</div>

This isn’t specific to Julia but applies to all languages that have object-orientation or user-defined data structures. There are two typical uses for structs/objects: (1) abstraction, and (2) encapsulation.

Abstraction is for when you have a domain-specific “object” that you want to reason about or operate on. Since you probably have a math background, consider that mathematics behaves similarly. You might say that numbers are the “built-in” data types of mathematics. Not using structs in Julia is the equivalent of doing “just algebra” in math. But then you start defining abstractions once you move to a more specific domain, e.g., geometry. You then ask questions like “At what point do two lines cross?” after you’ve _defined_ “line” and “point” as something you want to reason about. As such, the `Point` struct in the documentation is not just a trivial example. If you’re writing geometry-related code, you actually _want_ to define such structs to simplify your code.

Another aspect of abstractions is that they are often the key to generalization: consider that you could define the abstract behavior of points (via methods) which would then still apply if you wanted to formulate a `Point3D` or `PointSphericalCoordinates` struct later on.

The second reason to use structs is for the encapsulation and management of state. Consider [what happens internally](https://docs.sciml.ai/DiffEqDocs/stable/basics/integrator/#integrator) when you use `DifferentialEquations` to solve the ODE that you set up with `ModelingToolkit`:

```julia
integrator = init(prob, alg; kwargs...)

```

The `prob` here again is an example of “abstraction”, but the `integrator` is an object that holds the state of the solver between steps (the state vector, the current time, etc.)

Another example for “state management” is the situation where you find your function has 20 input parameters (which are then maybe passed to another function). To simplify the code, you can wrap all of these parameters into a struct to be able to pass it around more easily.

State management is a much trickier use of structs than abstraction, simply because management of state is basically _the_ problem of programming (even more so once you get to questions of concurrency). Managing state is hard, and virtually all non-trivial bugs stem from the mismanagement of state, which is probably why the example in the documentation deals with the easier “abstraction” use case.

An important part of encapsulation is that it can hide complexity by having private fields. For example, the `integrator` [only documents “useful” fields](https://docs.sciml.ai/DiffEqDocs/stable/basics/integrator/#Handling-Integrators). This is especially true in Julia, where it is quite common for the fields of structs to be considered completely private, maybe accessible as [properties](https://discourse.julialang.org/t/whats-the-difference-between-fields-and-properties/12495/5) or more commonly via [accessor methods](https://discourse.julialang.org/t/is-accessing-the-fields-of-a-struct-a-good-julia-style/23623/3). In many cases, [interfaces](https://docs.julialang.org/en/v1/manual/interfaces/) are defined purely in terms of methods.

Generally, “abstraction” structs should be immutable, while “state management” structs are mutable (or at least have mutable components), so Julia actually makes the distinction somewhat more clear than other languages by having the `mutable` keyword.

---

_[View the full topic](https://discourse.julialang.org/t/what-are-the-uses-of-some-of-the-numerous-types-in-julia-e-g-structs-modules/100708)._
