# Love Julia, but..... Type hierarchy is frustrating

**URL:** <https://discourse.julialang.org/t/love-julia-but-type-hierarchy-is-frustrating/39651>\
**Category:** General Usage\
**Created:** [May 17, 2020, 7:30pm UTC](https://discourse.julialang.org/t/love-julia-but-type-hierarchy-is-frustrating/39651 "2020-05-17T19:30:48Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jfchessa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jfchessa/32/14911_2.png) [@jfchessa](https://discourse.julialang.org/u/jfchessa)\
**Post date:** [May 17, 2020, 7:30pm UTC](https://discourse.julialang.org/t/love-julia-but-type-hierarchy-is-frustrating/39651/1 "2020-05-17T19:30:48Z")

</div>

Two things that I have so much frustration with in Julia are importing and all the type and abstract type hierarchy. For example why does the following happen?  
 ![Capture](https://global.discourse-cdn.com/julialang/original/3X/4/d/4d8ca431670c7ead4010eec2dfbadc9dfdaabb8b.jpeg)

But you need this to be robust to do multiple dispatch, no?

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [May 17, 2020, 7:51pm UTC](https://discourse.julialang.org/t/love-julia-but-type-hierarchy-is-frustrating/39651/2 "2020-05-17T19:51:52Z")

</div>

Do you have some intermediate types there? This works for me:

```julia
julia> abstract type A end
julia> struct B <: A; end
julia> b = B();
julia> typeof(b) <: A
true
julia> B <: A
true

```

---

<div class="post-metadata">

**Author:** ![tkluck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkluck/32/15769_2.png) [@tkluck](https://discourse.julialang.org/u/tkluck)\
**Post date:** [May 17, 2020, 7:55pm UTC](https://discourse.julialang.org/t/love-julia-but-type-hierarchy-is-frustrating/39651/3 "2020-05-17T19:55:06Z")

</div>

That seems… unexpected. In general, your expectation is absolutely right that these two things should give the same output.

I can’t try this out because `FEM` doesn’t seem to be a registered package. One possibility is that the variable `FEM` in your session does not point at the top-level module `Main.FEM`. Another is that someone wrote a method

```julia
show(io::IO, ::Type{FEM.GenProp}) = .... 

```

that writes a “wrong” display value.

Both of these are not very typical situations. You can help us help you by giving more specific context.

(PS. please [don’t post screenshots of code](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757/); you are excluding people who rely on screen readers and you are making it impossible for people who want to help you to copy-paste your code.)

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [May 17, 2020, 7:56pm UTC](https://discourse.julialang.org/t/love-julia-but-type-hierarchy-is-frustrating/39651/4 "2020-05-17T19:56:52Z")

</div>

I think @tkluck is onto something: two FEM modules are not necessarily one and the same. Main.FEM may be different from FEM.  
EDIT: And actually, what my point really was, the types defined in those two modules which appear to have the same name but are not really the same, are also _not the same_.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [May 17, 2020, 8:02pm UTC](https://discourse.julialang.org/t/love-julia-but-type-hierarchy-is-frustrating/39651/5 "2020-05-17T20:02:26Z")

</div>

Yes, I agree — this is most likely due to `include`ing your code files more than once. If you ever find yourself `include`ing the same file more than once it’s time to create a package so you can `import` or `using` it.

---

<div class="post-metadata">

**Author:** ![jfchessa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jfchessa/32/14911_2.png) [@jfchessa](https://discourse.julialang.org/u/jfchessa)\
**Post date:** [May 18, 2020, 1:09am UTC](https://discourse.julialang.org/t/love-julia-but-type-hierarchy-is-frustrating/39651/6 "2020-05-18T01:09:21Z")

</div>

I was suspicious of this. I think this is definitely the case. So they come from the same module (which I wrote so this is highly suspicious as well). But I think one was instantiated through the REPL as opposed to coming from being imported into another module. I am still a bit unclear about using vs import and that things seem to possibly be in a different scope in the REPL. I will try to isolate it and see what is the issue and if not at least I will have a simple complete example to post. Thanks!

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [May 18, 2020, 2:15am UTC](https://discourse.julialang.org/t/love-julia-but-type-hierarchy-is-frustrating/39651/7 "2020-05-18T02:15:56Z")

</div>

Yeah, it can definitely be confusing at first. The really important thing is that it’s totally fine to do `using Foo` and/or `import Foo` in as many places as you want–Julia will load the code exactly once no matter how many `using Foo` and `import Foo` you have. But `include("Foo.jl")` is more low-level: if you have `include("Foo.jl")` in two different places, you will end up actually including that code twice, potentially creating duplicate modules, types, and functions with the same names but different identities. This is a big difference from C++ where it’s common to `#include Foo.h` all over the place.

---

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [May 18, 2020, 7:14am UTC](https://discourse.julialang.org/t/love-julia-but-type-hierarchy-is-frustrating/39651/8 "2020-05-18T07:14:31Z")

</div>

PHP had the same issue before and came up with the [`include_once`](https://www.php.net/manual/en/function.include-once.php) function. Perhaps we could have something similar here?

This quote just came up in my mind: 🙂

> There’s already a _package_ for that.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 18, 2020, 7:40am UTC](https://discourse.julialang.org/t/love-julia-but-type-hierarchy-is-frustrating/39651/9 "2020-05-18T07:40:01Z")

</div>

> [@tk3369](#):
>
> Perhaps we could have something similar here?

Why? Packages are a much better way to organize code.

---

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [May 18, 2020, 7:46am UTC](https://discourse.julialang.org/t/love-julia-but-type-hierarchy-is-frustrating/39651/10 "2020-05-18T07:46:55Z")

</div>

I agree. Packages are definitely nicer but I suppose that some people just want to write script.

---

<div class="post-metadata">

**Author:** ![lostella](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lostella/32/356_2.png) [@lostella](https://discourse.julialang.org/u/lostella)\
**Post date:** [May 18, 2020, 8:10am UTC](https://discourse.julialang.org/t/love-julia-but-type-hierarchy-is-frustrating/39651/11 "2020-05-18T08:10:05Z")

</div>

In C/C++ people use preprocessor directives to avoid including things twice, as far as I remember (not sure whether things changed). Maybe a macro to be put on top of included files could do a similar thing?

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [May 18, 2020, 8:48am UTC](https://discourse.julialang.org/t/love-julia-but-type-hierarchy-is-frustrating/39651/12 "2020-05-18T08:48:25Z")

</div>

I really think `include()` should be banned from the REPL 🙈 I saw so many horrible workflows, errors and conclusions related to this practice… I don’t know where this comes from, I guess this is something what you do in Matlab regularly?

'fully agree with @Tamas_Papp that packages are meant for organising code.

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [May 18, 2020, 8:50am UTC](https://discourse.julialang.org/t/love-julia-but-type-hierarchy-is-frustrating/39651/13 "2020-05-18T08:50:52Z")

</div>

In Julia, the threshold to create a package is really low and I think everyone who does code development should learn how to do it.

Sorry for being off topic.

---

<div class="post-metadata">

**Author:** ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)\
**Post date:** [May 18, 2020, 9:07am UTC](https://discourse.julialang.org/t/love-julia-but-type-hierarchy-is-frustrating/39651/14 "2020-05-18T09:07:07Z")

</div>

I use `include()` for two purposes, and I don’t see how I could avoid using `include()` in these two cases:

- Scripting, i.e. to work on those pieces of code which are too application-specific for a package but too long to simply type it into the REPL.
- Package development. `Pkg.test()` has a big overhead, and `using Package` requires to restart the REPL every time I change the package.

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [May 18, 2020, 9:13am UTC](https://discourse.julialang.org/t/love-julia-but-type-hierarchy-is-frustrating/39651/15 "2020-05-18T09:13:59Z")

</div>

Have you ever tried [Revise.jl](https://github.com/timholy/Revise.jl)? It takes care of reloading changed functions.

Regarding your scripting workflow, this of course totally differs from mine. I create projects and package for every “project” I work on, no matter if it’s an actual Julia package, a quick throw-away analysis or an serious scientific analysis. For the latter two I prefer [DrWatson.jl](https://github.com/JuliaDynamics/DrWatson.jl).  
Code which is “too long to simply type it into the REPL” are in my opinion functions which should have a short name 😉

---

<div class="post-metadata">

**Author:** ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)\
**Post date:** [May 18, 2020, 9:31am UTC](https://discourse.julialang.org/t/love-julia-but-type-hierarchy-is-frustrating/39651/16 "2020-05-18T09:31:12Z")

</div>

> Code which is “too long to simply type it into the REPL” are in my opinion functions which should have a short name 😉

I fully agree, and I’m working hard to make my students and collaborators adopt this point of view 😄

A bit more precisely, I do write my scripts in the form of functions, but then I simply run `include("file.jl"); fun();` to execute my scripts. If I understand correctly, Revise.jl would allow me to omit the `include()`, but for me the `include()` is not big enough of a deal to start using a sophisticated package which I don’t really understand. And another issue with Revise is that it does not allow me to redefine types, which is often a deal-breaker for me.

---

<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:** [May 18, 2020, 9:42am UTC](https://discourse.julialang.org/t/love-julia-but-type-hierarchy-is-frustrating/39651/17 "2020-05-18T09:42:15Z")

</div>

> [@ettersi](#):
>
> but for me the `include()` is not big enough of a deal to start using a sophisticated package which I don’t really understand.

Just FYI, I’m “using” Revise.jl, and have _no_ understanding of how it works. I just put

```julia
@async begin
    sleep(0.1)
    @eval using Revise
end

```

in my startup.jl file, and never thought any more about it. To me, it just means that I don’t have to restart the REPL so much. There’s zero mental overhead in it to me. I’m actually slightly baffled when people suggest “using” Revise, since I cannot fathom why it’s not just in everyone’s startup file. I mean, what’s up with that?

---

<div class="post-metadata">

**Author:** ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)\
**Post date:** [May 18, 2020, 9:47am UTC](https://discourse.julialang.org/t/love-julia-but-type-hierarchy-is-frustrating/39651/18 "2020-05-18T09:47:46Z")

</div>

This works until you observe something which doesn’t make sense to you, and then you have one more software component which could be the source of the unexplained behaviour. I remember running into such issues last time I tried Revise. These issues might have been due to Revise or due to something completely unrelated, but the point is that I don’t know and I also don’t want to spend the time to find out.

Anyway, I’m digressing into an unnecessary rant. The main goal of my posts was to hear and share how people use Julia, and me for one I learnt something, so thanks everyone!

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [May 18, 2020, 9:50am UTC](https://discourse.julialang.org/t/love-julia-but-type-hierarchy-is-frustrating/39651/19 "2020-05-18T09:50:09Z")

</div>

I thought the [recommended configuration](https://timholy.github.io/Revise.jl/stable/config/#Earlier-versions-of-Revise-and/or-Julia-1) for Julia v1.4- was

```julia
atreplinit() do repl
    try
        @eval using Revise
        @async Revise.wait_steal_repl_backend()
    catch e
        @warn(e.msg)
    end
end

```

to avoid loading `Revise` when using Julia non-interactively

---

<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:** [May 18, 2020, 9:51am UTC](https://discourse.julialang.org/t/love-julia-but-type-hierarchy-is-frustrating/39651/20 "2020-05-18T09:51:30Z")

</div>

I put that statement in there some years ago, and never thought more about it, so it figures that it’s a bit out of date.

I’ll update it now, thanks!

[Next page](https://discourse.julialang.org/t/love-julia-but-type-hierarchy-is-frustrating/39651.md?page=2)
