# When you don't assign a variable in the inner constructor, how does it get assigned

**URL:** <https://discourse.julialang.org/t/when-you-dont-assign-a-variable-in-the-inner-constructor-how-does-it-get-assigned/61852>\
**Category:** General Usage\
**Created:** [May 26, 2021, 12:31pm UTC](https://discourse.julialang.org/t/when-you-dont-assign-a-variable-in-the-inner-constructor-how-does-it-get-assigned/61852 "2021-05-26T12:31:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon60034542](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@anon60034542](https://discourse.julialang.org/u/anon60034542)\
**Post date:** [May 26, 2021, 12:31pm UTC](https://discourse.julialang.org/t/when-you-dont-assign-a-variable-in-the-inner-constructor-how-does-it-get-assigned/61852/1 "2021-05-26T12:31:27Z")

</div>

Consider the following code:

```julia
struct Employee
    ID::Int32
    name::String
end

```

and I can create an object and call any field:

```julia
svetlana = Employee(1,"Queen Svetlana")
println(svetlana.name)

```

If I require validation, I can have an inner constructor:

```julia
struct Employee
    ID::Int32
    name::String

    function Employee(ID::Int32,name::String)
        if ID < 0
            error("ID is invalid!")
        end

        if isempty(name)
            error("Employee name cannot be blank!")
        end

        ID = ID
        name = name

        new(ID,name)
    end
end

```

When I remove the variable assignment in the constructor, how do the fields get assigned? I can still call `ID` or `name` and get the value:

```julia
struct Employee
    ID::Int32
    name::String

    function Employee(ID::Int32,name::String)
        if ID < 0
            error("ID is invalid!")
        end

        if isempty(name)
            error("Employee name cannot be blank!")
        end
        new(ID,name)
    end
end

```

The `ID` and `name` in this case are the ones I passed into the constructor, and once they’re validated I return them.

```julia
svetlana = Employee(1,"Queen Svetlana")
println(svetlana.name)

```

Why does calling `svetlana.name` and get the value?

---

<div class="post-metadata">

**Author:** ![paulmelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paulmelis/32/35063_2.png) [@paulmelis](https://discourse.julialang.org/u/paulmelis)\
**Post date:** [May 26, 2021, 12:41pm UTC](https://discourse.julialang.org/t/when-you-dont-assign-a-variable-in-the-inner-constructor-how-does-it-get-assigned/61852/2 "2021-05-26T12:41:15Z")

</div>

> [@anon60034542](#):
>
> When I remove the variable assignment in the constructor, how do the fields get assigned? I can still call `ID` or `name` and get the value:

Not sure I understand the question fully, but the `ID` and `name` fields do not get set on the struct by the assignments, but by the `new(ID, name)` call whose result (an `Employee`) you subsequently return from the constructor. Hence, the assignments are not useful (certainly not in the form `ID=ID` as you used).

---

<div class="post-metadata">

**Author:** ![eliascarv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eliascarv/32/22152_2.png) [@eliascarv](https://discourse.julialang.org/u/eliascarv)\
**Post date:** [May 26, 2021, 1:31pm UTC](https://discourse.julialang.org/t/when-you-dont-assign-a-variable-in-the-inner-constructor-how-does-it-get-assigned/61852/3 "2021-05-26T13:31:47Z")

</div>

In the internal constructor method, `ID` and `name` are just parameters.  
The special function `new()` creates an object with these parameters, therefore, they do not necessarily have to have the same name as the structure’s attributes. Ex:

```julia
struct Employee
    ID::Int32
    name::String

    function Employee(id::Int32, n::String)
        if id < 0
            error("ID is invalid!")
        end

        if isempty(n)
            error("Employee name cannot be blank!")
        end

        new(id, n)
    end
end

```

PS: Sorry for my english.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [May 26, 2021, 1:51pm UTC](https://discourse.julialang.org/t/when-you-dont-assign-a-variable-in-the-inner-constructor-how-does-it-get-assigned/61852/4 "2021-05-26T13:51:05Z")

</div>

> [@anon60034542](#):
>
> how do the fields get assigned?

To complement the other answers, the fields are filled in the order they are defined in the struct, by the arguments to `new`. Since `ID` is the first field in the struct it gets the value from the first argument to `new`, and so on.

---

<div class="post-metadata">

**Author:** ![anon60034542](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@anon60034542](https://discourse.julialang.org/u/anon60034542)\
**Post date:** [May 26, 2021, 2:34pm UTC](https://discourse.julialang.org/t/when-you-dont-assign-a-variable-in-the-inner-constructor-how-does-it-get-assigned/61852/5 "2021-05-26T14:34:13Z")

</div>

In the [documentation](https://docs.julialang.org/en/v1/manual/constructors/#Case-Study:-Rational), the inner constructor does have assignments (it does some calculations before it does the assignment).

To clarify, when I remove the assignments, how does `ID` and `name` still get its values assigned? I always assumed constructors were supposed to assign the values.

---

<div class="post-metadata">

**Author:** ![paulmelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paulmelis/32/35063_2.png) [@paulmelis](https://discourse.julialang.org/u/paulmelis)\
**Post date:** [May 26, 2021, 2:39pm UTC](https://discourse.julialang.org/t/when-you-dont-assign-a-variable-in-the-inner-constructor-how-does-it-get-assigned/61852/6 "2021-05-26T14:39:41Z")

</div>

> [@anon60034542](#):
>
> In the [documentation](https://docs.julialang.org/en/v1/manual/constructors/#Case-Study:-Rational), the inner constructor does have assignments (it does some calculations before it does the assignment).

In the example you refer to the values of `num` and `den` are recalculated, based on the value of `g`. That is why those assignments are there. But the values are set on the struct by the `new(num, den)` call.

> [@anon60034542](#):
>
> To clarify, when I remove the assignments, how does `ID` and `name` still get its values assigned? I always assumed constructors were supposed to assign the values.

No, it really is the `new()` call that assigns the values. You might be familiar with C++ for setting fields in constructors, but in Julia it works differently. You could just as easily have used `new(3, 4)` to set the `num` and `den` values of the struct. In fact, the _return value_ of the constructor is actually what is used:

```julia
julia> struct Employee
           ID::Int32
           name::String
           function Employee(ID::Int, name::String)
               return 1.23
           end
       end

julia> e = Employee(9, "abc")
1.23

julia> typeof(e)
Float64

```

---

<div class="post-metadata">

**Author:** ![anon60034542](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@anon60034542](https://discourse.julialang.org/u/anon60034542)\
**Post date:** [May 26, 2021, 2:48pm UTC](https://discourse.julialang.org/t/when-you-dont-assign-a-variable-in-the-inner-constructor-how-does-it-get-assigned/61852/7 "2021-05-26T14:48:20Z")

</div>

If I wanted to do some work on the string, like strip it to remove white space and call `title()`, then would the assignments make sense?

```julia
function Employee(ID::Int32, name::String)
    if isempty(strip(name))
        error("Employee name cannot be blank!")
    end
    name = title(name)
    new(ID,name)
end

```

---

<div class="post-metadata">

**Author:** ![paulmelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paulmelis/32/35063_2.png) [@paulmelis](https://discourse.julialang.org/u/paulmelis)\
**Post date:** [May 26, 2021, 2:49pm UTC](https://discourse.julialang.org/t/when-you-dont-assign-a-variable-in-the-inner-constructor-how-does-it-get-assigned/61852/8 "2021-05-26T14:49:55Z")

</div>

You could either assign the updated value to `name` and then pass that to `new()` or call `new(ID, title(name))` directly, it has the same end result.
