# How to work with julia's structs

**URL:** https://discourse.julialang.org/t/how-to-work-with-julias-structs/19783
**Category:** New to Julia
**Tags:** data\_structures
**Created:** [January 18, 2019, 1:46pm UTC](https://discourse.julialang.org/t/how-to-work-with-julias-structs/19783 "2019-01-18T13:46:16Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![algebrato](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algebrato/32/6778_2.png) [@algebrato](https://discourse.julialang.org/u/algebrato)
#### Post date: [January 18, 2019, 1:46pm UTC](https://discourse.julialang.org/t/how-to-work-with-julias-structs/19783/1 "2019-01-18T13:46:17Z")

</div>

Hello,

I am Stefano, Ph.D. Student in Physics. For my research project, I need to use Julia. I come from CPP and I have not entirely changed my mental approach to this new programming language. Now I am in front of weird behavior in the data `struct` manipulation.

Can I define a struct method in function of others? A suitable example is the very inflated `struct point.`  
Given its spherical coordinate (r, theta, phi) I want the automatically completion of the x, y, z method. I tried a thing like this: (just for x coordinate)

```julia
struct point{T}
  r::T 
  theta::T
  phi::T
  x::T
  function point(r,theta,phi)
    x = r * sin(theta) * cos(phi)
  end
end

```

But it does not work. Can someone give me a hint? Thanks!

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [January 18, 2019, 1:56pm UTC](https://discourse.julialang.org/t/how-to-work-with-julias-structs/19783/2 "2019-01-18T13:56:05Z")

</div>

Welcome,

please take a look at [PSA: how to quote code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530).

Regarding your problem, [Constructors · The Julia Language](https://docs.julialang.org/en/v1/manual/constructors/index.html) is likely good reading.

You could do for example:

```julia
julia> struct Point{T}
           r::T
           theta::T
           phi::T
           x::T
       end

julia> Point(r, theta, phi) = Point(r, theta, phi, r * sin(theta) * cos(phi))
Point

julia> Point(2.0, 0.3, 0.5)
Point{Float64}(2.0, 0.3, 0.5, 0.5186867601044616)

```

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [January 18, 2019, 2:21pm UTC](https://discourse.julialang.org/t/how-to-work-with-julias-structs/19783/3 "2019-01-18T14:21:35Z")

</div>

You could also define a function `x(p::Point) ` that takes a point as argument and calculates its x component (without storing it in the struct itself, which may not be necessary).

---

<div class="post-metadata">

### Author: ![algebrato](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algebrato/32/6778_2.png) [@algebrato](https://discourse.julialang.org/u/algebrato)
#### Post date: [January 18, 2019, 7:53pm UTC](https://discourse.julialang.org/t/how-to-work-with-julias-structs/19783/4 "2019-01-18T19:53:08Z")

</div>

Thank you for the answers!

I use this opportunity to ask:  
From the performance point of view is better either defined a constructor or use a separate function like proposed by @dpsanders

The constructor (I call it constructor but I don’t know if it is correct…) could be a thing like this:

```julia
julia> struct point
           r
           theta
           phi
           x
           y
           z
           point(r,theta,phi,x,y,z) = new(r,theta, phi, r*sin(theta)*cos(phi), r*sin(theta)*sin(phi), r*cos(theta))
       end

```

What is the better solution from the point of view of performance? (with “better perfomance” i means, in term of memory usage, some tricky things that i don’t know, ecc ecc)

---

<div class="post-metadata">

### Author: ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)
#### Post date: [January 18, 2019, 8:20pm UTC](https://discourse.julialang.org/t/how-to-work-with-julias-structs/19783/5 "2019-01-18T20:20:06Z")

</div>

Another possibility is a [functor](https://docs.julialang.org/en/v1/manual/methods/#Function-like-objects-1) where the struct itself can also act as a function:

```julia
struct Point{T}
    r::T
    θ::T
    ϕ::T
end

function(p::Point)()
   x = p.r * sin(p.θ) * cos(p.ϕ) 
    y = 1.0
   return [x,y] 
end

# Point(2., 0.3, 0.5) 

Point(2., 0.3, 0.5)()

```

---

<div class="post-metadata">

### Author: ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)
#### Post date: [January 18, 2019, 8:33pm UTC](https://discourse.julialang.org/t/how-to-work-with-julias-structs/19783/6 "2019-01-18T20:33:16Z")

</div>

- Structs are used to create types. It is recommended to start type names with a capital letter. In this case `Point` instead of `point`.
- By not specifying a type parameter, you may end up with a mix of datatypes. Consider specifying `struct Point{T} so that the point coordinates are either all integers or all floats.
- By defining a struct with 6 fields instead of 3 fields you double the memory required to store such structs. This might make sense if your application has to compute the x, y, and z coordinates of the same point many times, but in general, computing those coordinates when needed should be fine.
- Your inner constructor function requires 6 input parameters, but the last three (that is, x, y and z) are not used to compute the stored values. It would be better to only have one outer constructor function that only takes 3 parameters.

---

<div class="post-metadata">

### Author: ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)
#### Post date: [January 18, 2019, 8:35pm UTC](https://discourse.julialang.org/t/how-to-work-with-julias-structs/19783/7 "2019-01-18T20:35:25Z")

</div>

> [@algebrato](#):
>
> What is the better solution from the point of view of performance? (with “better perfomance” i means, in term of memory usage, some tricky things that i don’t know, ecc ecc)

It really depends on your problem. If you’ll access `x`, `y` and `z` repeatedly, it’s better to calculate them once and store them (avoid repeated calls to `sin` and `cos`). If you don’t always need these variables, it might be better to calculate them on demand. You can also combine these two approaches and calculate them lazily, i.e. on first invocation. If you have tons of points, then memory can become an issue.

Unless you know beforehand that this will cause performance issues, I wouldn’t really worry about performance at this point… write the code in a way you’re comfortable with (your current suggestion looks fine to me), and once your project has matured a bit (and you’ve become more proficient with Julia) you can do some performance testing and see where you need to optimize things.

(With that said, note that you can speed up that constructor a bit by reusing values of `sin(theta)` etc. There’s also a `sincos` method which calculates both `sin` and `cos` at the same time (more efficient).)
