# Types must be first defined then called in a module? Why?

**URL:** <https://discourse.julialang.org/t/types-must-be-first-defined-then-called-in-a-module-why/114497>\
**Category:** General Usage\
**Tags:** question, type, module\
**Created:** [May 21, 2024, 8:22am UTC](https://discourse.julialang.org/t/types-must-be-first-defined-then-called-in-a-module-why/114497 "2024-05-21T08:22:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [May 21, 2024, 8:22am UTC](https://discourse.julialang.org/t/types-must-be-first-defined-then-called-in-a-module-why/114497/1 "2024-05-21T08:22:05Z")

</div>

```julia
module MyModule
    
function myfun(t::MyType)
    println(plusone(t.x))
end

struct MyType
    x::Float64
end

function plusone(x)
    x + 1
end

end

```

```julia
ERROR: UndefVarError: `MyType` not defined 

```

---

<div class="post-metadata">

**Author:** ![Tarny\_GG\_Channie](https://avatars.discourse-cdn.com/v4/letter/t/3bc359/32.png) [@Tarny\_GG\_Channie](https://discourse.julialang.org/u/Tarny_GG_Channie)\
**Post date:** [May 21, 2024, 8:32am UTC](https://discourse.julialang.org/t/types-must-be-first-defined-then-called-in-a-module-why/114497/2 "2024-05-21T08:32:22Z")

</div>

Your type doesn’t exist yet, but it’s already used. The compiler/interpreter of programming languages typically don’t look ahead of time to see that it’s defined somewhere. This design choice is present in languages like c++ too. I think it has to do with the idea that a code shouldn’t look ahead.

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [May 21, 2024, 8:34am UTC](https://discourse.julialang.org/t/types-must-be-first-defined-then-called-in-a-module-why/114497/3 "2024-05-21T08:34:05Z")

</div>

That’s different with the feature of a function?

---

<div class="post-metadata">

**Author:** ![Tarny\_GG\_Channie](https://avatars.discourse-cdn.com/v4/letter/t/3bc359/32.png) [@Tarny\_GG\_Channie](https://discourse.julialang.org/u/Tarny_GG_Channie)\
**Post date:** [May 21, 2024, 8:34am UTC](https://discourse.julialang.org/t/types-must-be-first-defined-then-called-in-a-module-why/114497/4 "2024-05-21T08:34:51Z")

</div>

If you remove the type annotation (of the function argument), it would work.

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [May 21, 2024, 8:36am UTC](https://discourse.julialang.org/t/types-must-be-first-defined-then-called-in-a-module-why/114497/5 "2024-05-21T08:36:36Z")

</div>

Yes. But do you think that’s inconvenient? Does that mean it’s better to (or we have to) put all of the type definitions on the top of a module?

---

<div class="post-metadata">

**Author:** ![Tarny\_GG\_Channie](https://avatars.discourse-cdn.com/v4/letter/t/3bc359/32.png) [@Tarny\_GG\_Channie](https://discourse.julialang.org/u/Tarny_GG_Channie)\
**Post date:** [May 21, 2024, 8:38am UTC](https://discourse.julialang.org/t/types-must-be-first-defined-then-called-in-a-module-why/114497/6 "2024-05-21T08:38:45Z")

</div>

That’s a common convention. I myself have been supportive of features requiring the code to look ahead such as attribute inference too but the idea is not very popular.

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [May 21, 2024, 8:40am UTC](https://discourse.julialang.org/t/types-must-be-first-defined-then-called-in-a-module-why/114497/7 "2024-05-21T08:40:32Z")

</div>

I didn’t realize this problem until today, which confused me very much.

---

<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:** [May 21, 2024, 11:40am UTC](https://discourse.julialang.org/t/types-must-be-first-defined-then-called-in-a-module-why/114497/9 "2024-05-21T11:40:10Z")

</div>

The idea is “All the symbols in the execution context must be defined.” It would be strange behavior if you saw this in a function

```julia
function my_func(vec)
    res = map(f, vec)
    f = (x) -> sin(x)
    return res
end 

```

Because `f` is reference before assignment.

In global scope, same rules apply.

The first line of a method is its signature and that signature has to be resolved at time of definition so that it can be added to the MethodTable for dispatching. So each Type annotation needs to have been defined already in order to not get a reference before assignment type error.

Now, you CAN have undefined symbol in an unexecuted context, for example, the method’s body. So this works:

```julia
f() = MyType()
struct MyType end
f() 

```

Because the calls inside `f` don’t get resolved until runtime.
