# Naming conventions for structs and constructors

**URL:** <https://discourse.julialang.org/t/naming-conventions-for-structs-and-constructors/106568>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 22, 2023, 10:27am UTC](https://discourse.julialang.org/t/naming-conventions-for-structs-and-constructors/106568 "2023-11-22T10:27:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [November 22, 2023, 10:27am UTC](https://discourse.julialang.org/t/naming-conventions-for-structs-and-constructors/106568/1 "2023-11-22T10:27:47Z")

</div>

I often have a struct and one or more constructors for the struct, e.g.

```julia
"""
    mutable struct InitialState

This struct stores the state of the model at the beginning of an iteration of the learning
algorithm. It is updated at the end of each iteration.

$(TYPEDFIELDS)
"""
mutable struct InitialState
    t0::Float64      
    t_sim::Float64
    u_est::Float64 
end

"""
     InitialState(; Γ = 1.0, Γest=1.0, Δα=0.0, Δα_est=0.0, offset=0.0)

Create an initial state for the given parameters.
"""
function InitialState(; Γ = 1.0, Γest=1.0, Δα=0.0, Δα_est=0.0, offset=0.0)
    init = InitialState(0,0,0)
    init.t0 = 0
    # T = 1/(se.w_ex/2pi)
    # init.t_sim = round(se.t_iter_alpha/T) * T # about 10min
    ...
    init
end

function simulate(initial::InitialState)
    println(initial.t0)
end

```

While the code works fine, I get problems with vscode and with Documenter.

vscode complains if I use `init::InitialState` in a function signature. It thinks InitialState is a function and not a type (well, it is both).

With Documenter I get the same problem, it cannot distinguish between the type and the function  
and if I document both it complains about a duplicate definition.

What is best practice? Always use different names for the type and the functions that construct the  
type?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [November 22, 2023, 11:00am UTC](https://discourse.julialang.org/t/naming-conventions-for-structs-and-constructors/106568/2 "2023-11-22T11:00:53Z")

</div>

> [@ufechner7](#):
>
> vscode complains if I use `init::InitialState` in a function signature. It thinks InitialState is a function and not a type (well, it is both).

With your example above I can’t reproduce this, everything works as expected for me. Can you provide an MWE?

> [@ufechner7](#):
>
> With Documenter I get the same problem, it cannot distinguish between the type and the function  
> and if I document both it complains about a duplicate definition.

Can you provide an MWE? In the example above there is just one docstring, and no information on how you try to incorporate these into Documenters `@docs` block.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [November 22, 2023, 11:26am UTC](https://discourse.julialang.org/t/naming-conventions-for-structs-and-constructors/106568/3 "2023-11-22T11:26:59Z")

</div>

OK, I update the example above to have two docstrings…

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [November 22, 2023, 11:45am UTC](https://discourse.julialang.org/t/naming-conventions-for-structs-and-constructors/106568/4 "2023-11-22T11:45:31Z")

</div>

What about:

> [@fredrikekre](#):
>
> no information on how you try to incorporate these into Documenters `@docs` block.

For example, this works just fine:

````julia
```@docs
TestPackage.InitialState # struct type docs
TestPackage.InitialState() # constructor docs
```

````

Your updated example doesn’t show any warnings like you mention:

> [@ufechner7](#):
>
> f I document both it complains about a duplicate definition.

[It would be useful if you can provide examples that reproduce your problems.](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757)

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [November 22, 2023, 11:55am UTC](https://discourse.julialang.org/t/naming-conventions-for-structs-and-constructors/106568/5 "2023-11-22T11:55:40Z")

</div>

Per point 8 of that link, maybe there’s a discrepancy in the environments influencing how this problem happens?

---

<div class="post-metadata">

**Author:** ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)\
**Post date:** [November 22, 2023, 12:19pm UTC](https://discourse.julialang.org/t/naming-conventions-for-structs-and-constructors/106568/6 "2023-11-22T12:19:05Z")

</div>

Sometimes I get strange behaviors from the linter. Not reproducible, or I’d open an issue. The only way I could fix it was to uninstall and reinstall the extension.🤷‍♂️

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [November 22, 2023, 12:30pm UTC](https://discourse.julialang.org/t/naming-conventions-for-structs-and-constructors/106568/7 "2023-11-22T12:30:17Z")

</div>

It works for me now. I put the struct definition and the constructor functions in the same file. This seams to help. Also thanks for the tip to refer to the function in Documenter as `InitialState()` and to the type as `InitialState`. This makes Documenter happy.

Conclusion: Different names are not required.
