# Structs... creating a struct without passing all variables

**URL:** <https://discourse.julialang.org/t/structs-creating-a-struct-without-passing-all-variables/36588>\
**Category:** General Usage\
**Created:** [March 27, 2020, 9:13am UTC](https://discourse.julialang.org/t/structs-creating-a-struct-without-passing-all-variables/36588 "2020-03-27T09:13:49Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![mayar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mayar/32/16426_2.png) [@mayar](https://discourse.julialang.org/u/mayar)\
**Post date:** [March 27, 2020, 9:13am UTC](https://discourse.julialang.org/t/structs-creating-a-struct-without-passing-all-variables/36588/1 "2020-03-27T09:13:49Z")

</div>

hello guys,  
I have defined a struct with 5 variables  
then  
I created one and I passed 3 out of 5 variables  
now I am getting error  
How do I define a struct without needing to define all variables

Thank you

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [March 27, 2020, 9:25am UTC](https://discourse.julialang.org/t/structs-creating-a-struct-without-passing-all-variables/36588/2 "2020-03-27T09:25:05Z")

</div>

You probably want to define a constructor with default arguments, like:

```julia
julia> struct MyType
           a
           b
           c
       end

julia> MyType(a, b) = MyType(a, b, "default value")
MyType

julia> MyType(1,2)
MyType(1, 2, "default value")

```

---

<div class="post-metadata">

**Author:** ![mayar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mayar/32/16426_2.png) [@mayar](https://discourse.julialang.org/u/mayar)\
**Post date:** [March 27, 2020, 9:27am UTC](https://discourse.julialang.org/t/structs-creating-a-struct-without-passing-all-variables/36588/3 "2020-03-27T09:27:26Z")

</div>

Okay… I will check it now and give you feedback  
Thank you

---

<div class="post-metadata">

**Author:** ![mayar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mayar/32/16426_2.png) [@mayar](https://discourse.julialang.org/u/mayar)\
**Post date:** [March 27, 2020, 10:00am UTC](https://discourse.julialang.org/t/structs-creating-a-struct-without-passing-all-variables/36588/4 "2020-03-27T10:00:36Z")

</div>

It didnot work until  
I declared my type as “mutable struct”  
and  
I also had to give all variable a zero value as an initial value  
then  
I worte

mystruct.x=50

look here please

```julia
![p33|690x401](upload://1t5SxtMZj1Dgw0uuBh7m5ZdZgKs.png) 

```

---

<div class="post-metadata">

**Author:** ![mayar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mayar/32/16426_2.png) [@mayar](https://discourse.julialang.org/u/mayar)\
**Post date:** [March 27, 2020, 10:01am UTC](https://discourse.julialang.org/t/structs-creating-a-struct-without-passing-all-variables/36588/5 "2020-03-27T10:01:06Z")

</div>

![p33](https://global.discourse-cdn.com/julialang/original/3X/8/1/81f51e7be10a4a6fc6325d84be5aa4e4fb0a8bc7.png)

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [March 27, 2020, 10:13am UTC](https://discourse.julialang.org/t/structs-creating-a-struct-without-passing-all-variables/36588/6 "2020-03-27T10:13:16Z")

</div>

I don’t think you understood my suggestion, here’s an example with your struct defintion:

```julia
# Define the struct - this will automatically provide a constructor that takes all fields
struct lineStruct2
    buses_line::Vector{Int}
    res_line::Float64
    ind_line::Float64
    imp_mag_line::Float64
    imp_angle_line::Float64
    p_line::Float64
    q_line::Float64
    state_line::String
end

# Define an outer constructor that only requires the first three inputs, using default values for the remaining inputs
lineStruct2(buses_line, res_line, ind_line) = lineStruct2(buses_line, res_line,
    ind_line, 0.0, 0.0, 0.0, 0.0, "overloaded")

# Test that it works
lineStruct2([1,2,3], 0.0, 0.0)

```

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [March 27, 2020, 10:46am UTC](https://discourse.julialang.org/t/structs-creating-a-struct-without-passing-all-variables/36588/7 "2020-03-27T10:46:03Z")

</div>

use Parameters.jl

---

<div class="post-metadata">

**Author:** ![mayar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mayar/32/16426_2.png) [@mayar](https://discourse.julialang.org/u/mayar)\
**Post date:** [March 27, 2020, 11:51am UTC](https://discourse.julialang.org/t/structs-creating-a-struct-without-passing-all-variables/36588/8 "2020-03-27T11:51:16Z")

</div>

I get an invalid redefinition

look at this pls

 ![invalid](https://global.discourse-cdn.com/julialang/original/2X/1/141b0d406f00dc470816e1910bfae0e32a592a3e.png)

---

<div class="post-metadata">

**Author:** ![mayar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mayar/32/16426_2.png) [@mayar](https://discourse.julialang.org/u/mayar)\
**Post date:** [March 27, 2020, 11:51am UTC](https://discourse.julialang.org/t/structs-creating-a-struct-without-passing-all-variables/36588/9 "2020-03-27T11:51:39Z")

</div>

I tried with both mutable and without it

---

<div class="post-metadata">

**Author:** ![mayar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mayar/32/16426_2.png) [@mayar](https://discourse.julialang.org/u/mayar)\
**Post date:** [March 27, 2020, 11:54am UTC](https://discourse.julialang.org/t/structs-creating-a-struct-without-passing-all-variables/36588/10 "2020-03-27T11:54:55Z")

</div>

I just pressed enter in the REPL and the error did not show again why is that ?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 27, 2020, 11:56am UTC](https://discourse.julialang.org/t/structs-creating-a-struct-without-passing-all-variables/36588/11 "2020-03-27T11:56:17Z")

</div>

You have to restart Julia.

Also typenames should be CapitalCase (by convention). So call it `LineStruct`, not `lineStruct`.

---

<div class="post-metadata">

**Author:** ![mayar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mayar/32/16426_2.png) [@mayar](https://discourse.julialang.org/u/mayar)\
**Post date:** [March 27, 2020, 12:16pm UTC](https://discourse.julialang.org/t/structs-creating-a-struct-without-passing-all-variables/36588/12 "2020-03-27T12:16:21Z")

</div>

yes, it worked thank you …  
now  
when I type for example

L2=lineStruct2([1,2,3],10,10)

I get an error that L2 is undefined

Why ?!

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [March 27, 2020, 1:17pm UTC](https://discourse.julialang.org/t/structs-creating-a-struct-without-passing-all-variables/36588/13 "2020-03-27T13:17:50Z")

</div>

That seems unlikely. It would help if you could give a MWE with code written in mono-spaced font

````julia
```
using backticks like the above
your code here
```

````

to help isolate the problem.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [March 27, 2020, 1:29pm UTC](https://discourse.julialang.org/t/structs-creating-a-struct-without-passing-all-variables/36588/14 "2020-03-27T13:29:47Z")

</div>

I would highy recommend you read through the relevant [section of the manual](https://docs.julialang.org/en/v1/manual/types/).

As @DNF says you cannot re-define types within the same Julia session:

```julia
julia> struct MyType
           a::Float64
       end

julia> struct MyType
           no_wait_I_changed_my_mind
       end
ERROR: invalid redefinition of constant MyType
Stacktrace:
 [1] top-level scope at REPL[2]:1

```

Therefore, once you’ve defined `LineStruct` you can only change that after restarting Julia. That’s also why in my example it was called `LineStruct2`, because `LineStruct` was already defined in the session when I created the MWE.

Can you restart Julia and run the following:

```julia
struct LineStruct
    buses_line::Vector{Int}
    res_line::Float64
    ind_line::Float64
    imp_mag_line::Float64
    imp_angle_line::Float64
    p_line::Float64
    q_line::Float64
    state_line::String
end

LineStruct(buses_line, res_line, ind_line) = LineStruct(buses_line, res_line,
    ind_line, 0.0, 0.0, 0.0, 0.0, "overloaded")

l2 = LineStruct2([1,2,3], 0.0, 0.0)

```

Note that as per @DNF’s suggestion I’ve capitalized `LineStruct` (the type) here, but lowercased `l2` (an instance of the type).

This should work and do what you want.

---

<div class="post-metadata">

**Author:** ![mayar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mayar/32/16426_2.png) [@mayar](https://discourse.julialang.org/u/mayar)\
**Post date:** [March 27, 2020, 2:25pm UTC](https://discourse.julialang.org/t/structs-creating-a-struct-without-passing-all-variables/36588/15 "2020-03-27T14:25:39Z")

</div>

Thanks alot… it worked and I will check the link too now Thanks again
