# How is it that new Julia programmers tend to abuse type annotations?

**URL:** <https://discourse.julialang.org/t/how-is-it-that-new-julia-programmers-tend-to-abuse-type-annotations/108465>\
**Category:** Teaching & Outreach\
**Created:** [January 7, 2024, 1:40pm UTC](https://discourse.julialang.org/t/how-is-it-that-new-julia-programmers-tend-to-abuse-type-annotations/108465 "2024-01-07T13:40:01Z")\
**Posts on this page:** 20\
**Page:** 3

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [January 9, 2024, 12:03pm UTC](https://discourse.julialang.org/t/how-is-it-that-new-julia-programmers-tend-to-abuse-type-annotations/108465/41 "2024-01-09T12:03:21Z")

</div>

I’m sure it helps some people, but it certainly doesn’t solve the problem. The fundamental problem is just lack of knowledge. The solution is learning more, and that takes time. Tooling that displays information in a more usable way is of course useful for learning, but it won’t end the problem (especially because most newcomers have never heard of Cthulhu)

---

<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:** [January 9, 2024, 12:31pm UTC](https://discourse.julialang.org/t/how-is-it-that-new-julia-programmers-tend-to-abuse-type-annotations/108465/42 "2024-01-09T12:31:24Z")

</div>

> [@Mason](#):
>
> it’s pretty natural to just respond with “well, I’ll just put type annotations everywhere, it’s not that hard to do and maybe it’ll speed things up”

I agree. Also, superfluous type annotations are one of the first habits that users coming from other programming languages drop. No harm done, other than a bit of extra wear and tear on the keyboard. _[I wrote “extra typing” first then reread the sentence. 😉]_

---

<div class="post-metadata">

**Author:** ![xiaoxi](https://avatars.discourse-cdn.com/v4/letter/x/a9adbd/32.png) [@xiaoxi](https://discourse.julialang.org/u/xiaoxi)\
**Post date:** [January 9, 2024, 1:41pm UTC](https://discourse.julialang.org/t/how-is-it-that-new-julia-programmers-tend-to-abuse-type-annotations/108465/43 "2024-01-09T13:41:42Z")

</div>

One of the first things I learned in TypeScript was to add enough type annotations to prevent the compiler from inferring any of my variables as Any. To do this, I needed a tool that would give me real-time feedback about whether a variable was inferred as Any or not. With that information, I quickly developed the intuition to know where I should add type annotations.

I can imagine that newcomers to Julia might experience the same learning process if the Julia VS Code extension provides real-time feedback about whether a variable is inferred as Any or not.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [January 9, 2024, 7:10pm UTC](https://discourse.julialang.org/t/how-is-it-that-new-julia-programmers-tend-to-abuse-type-annotations/108465/44 "2024-01-09T19:10:37Z")

</div>

I think the Julia equivalent of that is using `@code_warntype` and fixing your code until the red or yellow text goes away. I wish it would do more to highlight abstract parameters such as in `Vector{Any}`.

![image](https://global.discourse-cdn.com/julialang/original/3X/4/0/40d282a297900205edec88e412272c4399a360b1.png)

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [January 9, 2024, 7:31pm UTC](https://discourse.julialang.org/t/how-is-it-that-new-julia-programmers-tend-to-abuse-type-annotations/108465/45 "2024-01-09T19:31:53Z")

</div>

Well that’s not showing anything red because you didn’t do anything involving `vec` that would cause any performance trouble.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/9/9998056132c50710792d75a31b4b1d13cf44dfe5.png)

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [January 9, 2024, 7:38pm UTC](https://discourse.julialang.org/t/how-is-it-that-new-julia-programmers-tend-to-abuse-type-annotations/108465/46 "2024-01-09T19:38:41Z")

</div>

One would prefer likely prefer `foo` to return `Vector{Float64}`. Coming from another language, it may be confusing that Julia does not infer a more specific element type such in `bar` below.

```julia
julia> bar(x) = [x, 5, 3.5]
bar (generic function with 1 method)

julia> bar(2)
3-element Vector{Float64}:
 2.0
 5.0
 3.5

julia> foo(2)
3-element Vector{Any}:
 2
 5
 3.5

```

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [January 9, 2024, 7:48pm UTC](https://discourse.julialang.org/t/how-is-it-that-new-julia-programmers-tend-to-abuse-type-annotations/108465/47 "2024-01-09T19:48:57Z")

</div>

> [@mkitti](#):
>
> Coming from another language, it may be confusing that Julia does not infer a more specific element type such in `bar` below.

I understand what you’re getting at, but in this case I don’t think it has anything to do with type inference. It just depends on whether the user understands that `[]` is syntax sugar for `Any[]`.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [January 9, 2024, 7:57pm UTC](https://discourse.julialang.org/t/how-is-it-that-new-julia-programmers-tend-to-abuse-type-annotations/108465/48 "2024-01-09T19:57:59Z")

</div>

It depends on the language semnatics. Consider this type inference example from Rust:

> **[Inference - Rust By Example](https://doc.rust-lang.org/rust-by-example/types/inference.html)**
>
> Rust by Example (RBE) is a collection of runnable examples that illustrate various Rust concepts and standard libraries.

```rust
fn main() {
    // Because of the annotation, the compiler knows that `elem` has type u8.
    let elem = 5u8;

    // Create an empty vector (a growable array).
    let mut vec = Vec::new();
    // At this point the compiler doesn't know the exact type of `vec`, it
    // just knows that it's a vector of something (`Vec<_>`).

    // Insert `elem` in the vector.
    vec.push(elem);
    // Aha! Now the compiler knows that `vec` is a vector of `u8`s (`Vec<u8>`)
    // TODO ^ Try commenting out the `vec.push(elem)` line

    println!("{:?}", vec);
}

```

A Rust user coming to Julia might wonder why Julian type inference is not as smart.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [January 9, 2024, 8:27pm UTC](https://discourse.julialang.org/t/how-is-it-that-new-julia-programmers-tend-to-abuse-type-annotations/108465/49 "2024-01-09T20:27:54Z")

</div>

> [@mkitti](#):
>
> A Rust user coming to Julia might wonder why Julian type inference is not as smart.

But that would be a misunderstanding of Julia semantics. In Julia, the element type of an array is determined at construction time—pushing into a Julia vector will never change the type of a vector. There is no type inference involved in determining the element type of a vector. Since `[]` is sugar for `Vector{Any}()`, the element type must be `Any`.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [January 9, 2024, 8:34pm UTC](https://discourse.julialang.org/t/how-is-it-that-new-julia-programmers-tend-to-abuse-type-annotations/108465/50 "2024-01-09T20:34:32Z")

</div>

Yeah, the type inference here is a distraction. If we had chosen `Vector` semantics that behaved like Rust’s vector, the compiler would have no trouble analyzing it in the exact same way.

```julia
julia> using MicroCollections, BangBang

julia> code_typed(Tuple{}; optimize=false) do 
           elem = Int8(5)
           vec = EmptyVector()
           vec = push!!(vec, elem)
       end
1-element Vector{Any}:
 CodeInfo(
1 ─ (elem = Main.Int8(5))::Core.Const(5)
│ (vec = Main.EmptyVector())::Core.Const(Union{}[])
│ %3 = Main.push!!(vec::Core.Const(Union{}[]), elem::Core.Const(5))::Vector{Int8}
│ (vec = %3)::Vector{Int8}
└── return %3
) => Vector{Int8}

```

---

<div class="post-metadata">

**Author:** ![Barget](https://avatars.discourse-cdn.com/v4/letter/b/94ad74/32.png) [@Barget](https://discourse.julialang.org/u/Barget)\
**Post date:** [January 9, 2024, 10:10pm UTC](https://discourse.julialang.org/t/how-is-it-that-new-julia-programmers-tend-to-abuse-type-annotations/108465/51 "2024-01-09T22:10:42Z")

</div>

I’d like to add another possibility (couldn’t find it after skimming through the answers): in the “static typing” camp, type annotation is considered helpful, for this help the programmer to understand what arguments the function is accepting. Thus, if one’s not well versed into the abstract type notion, it’s possible one’s unwillingly over-restricting the method as a result of using concrete types.

Put it differently, getting a clean code is considered good practice (if not accepted as a default, then painfully learnt during your early carrer), and some think static typing helps for that (see e.g. rationale behind TypeScript), hence naive but clumsy type annotation. Then, you eventually learn to think through type hierarchies, and stop over-specifying types.

---

<div class="post-metadata">

**Author:** ![fer2tdt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fer2tdt/32/47427_2.png) [@fer2tdt](https://discourse.julialang.org/u/fer2tdt)\
**Post date:** [January 10, 2024, 7:25am UTC](https://discourse.julialang.org/t/how-is-it-that-new-julia-programmers-tend-to-abuse-type-annotations/108465/52 "2024-01-10T07:25:56Z")

</div>

On the other hand, **my recommendation** is that controlling the “Type” I am using to represent my data is NOT an option, it is always necessary. Even if you don’t explicitly state it. Precisely because we want to have maximum confidence in the calculations we perform.  
The “Julia default type” in the function parameters is just an option.

---

<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:** [January 10, 2024, 10:58am UTC](https://discourse.julialang.org/t/how-is-it-that-new-julia-programmers-tend-to-abuse-type-annotations/108465/53 "2024-01-10T10:58:52Z")

</div>

> [@fer2tdt](#):
>
> The “Julia default type” in the function parameters is just an option.

Naturally you can use Julia the way you like, but note that because of multiple dispatch, it is idiomatic to write Julia code for which you simply **will not know the type** of function arguments.

But that is fine, all you need to assume is that they satisfy some (informally defined) interface. This allows a method from package A to work on arguments defined in packages B and C, withoutcooperation from said packages for this explicit purpose.

[![](https://global.discourse-cdn.com/julialang/original/3X/c/5/c5a2f02c81c376da005f8610ce9cdd4ad47cfdf4.jpeg "JuliaCon 2019 | The Unreasonable Effectiveness of Multiple Dispatch | Stefan Karpinski") ](https://www.youtube.com/watch?v=kc9HwsxE1OY)

---

<div class="post-metadata">

**Author:** ![fer2tdt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fer2tdt/32/47427_2.png) [@fer2tdt](https://discourse.julialang.org/u/fer2tdt)\
**Post date:** [January 10, 2024, 12:38pm UTC](https://discourse.julialang.org/t/how-is-it-that-new-julia-programmers-tend-to-abuse-type-annotations/108465/54 "2024-01-10T12:38:13Z")

</div>

I allways know the nature of the input data. I allways study which is the best computer representation for the operations to avoid errors on intermediate operations. And I allways have a sample of similar final data.  
Then you can try the code and results with a Concrete Type.  
If you don’t know enough about the input data… you must research before.

---

<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:** [January 10, 2024, 12:43pm UTC](https://discourse.julialang.org/t/how-is-it-that-new-julia-programmers-tend-to-abuse-type-annotations/108465/55 "2024-01-10T12:43:51Z")

</div>

> [@fer2tdt](#):
>
> I allways know the nature of the input data.

Good for you! Again, it is possible to program in Julia with that approach, but you are not reaping the benefits of the language. You might as well just use C.

Eg consider

```julia
nonsense(x, y, z) = √(x + y^2) / z

```

which, as defined, will work with all kinds of real number type (floats, incl `BigFloat` if you want to test), `ForwardDiff.Dual` (which usually adds a type tag from the outermost caller), complex numbers, quaternions, etc. So you can write it once, and reuse.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 10, 2024, 1:12pm UTC](https://discourse.julialang.org/t/how-is-it-that-new-julia-programmers-tend-to-abuse-type-annotations/108465/56 "2024-01-10T13:12:09Z")

</div>

> [@Tamas\_Papp](#):
>
> > [@fer2tdt](#):
> >
> > I allways know the nature of the input data.
> 
> Good for you! Again, it is possible to program in Julia with that approach, but you are not reaping the benefits of the language. You might as well just use C.

That’s a little too harsh, I think. Sometimes you are writing code that you want to re-use over and over, in which case you want to be generic, and sometimes you are writing code that’s designed only for a specific project. Often, you decide after the fact that you want to re-use the latter, in which case Julia allows you to gradually make it more generic. And even for single-use code, you still benefit from re-usable code that _other_ projects developed, as well as interactivity and high-level features of Julia that are not available in C.

If you feel more comfortable programming Julia with explicit concrete type declarations for everything, that’s totally fine. Just be aware that this is not generally necessary for performance (at least for local variables and function arguments), and that you may want to relax your types at some point to allow the code to become more generic.

---

<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:** [January 10, 2024, 1:18pm UTC](https://discourse.julialang.org/t/how-is-it-that-new-julia-programmers-tend-to-abuse-type-annotations/108465/57 "2024-01-10T13:18:10Z")

</div>

> [@stevengj](#):
>
> That’s a little too harsh, I think.

Indeed. Sorry being sarcastic. I was triggered by the “I always know everything”, but should have responded more gently.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 10, 2024, 5:08pm UTC](https://discourse.julialang.org/t/how-is-it-that-new-julia-programmers-tend-to-abuse-type-annotations/108465/58 "2024-01-10T17:08:18Z")

</div>

Split this topic on January 10, 2024, 5:08pm UTC

4 posts were split to a new topic: [Learning Julia for scientists who are beginning programmers](https://discourse.julialang.org/t/learning-julia-for-scientists-who-are-beginning-programmers/108638)

---

<div class="post-metadata">

**Author:** ![fer2tdt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fer2tdt/32/47427_2.png) [@fer2tdt](https://discourse.julialang.org/u/fer2tdt)\
**Post date:** [January 10, 2024, 3:58pm UTC](https://discourse.julialang.org/t/how-is-it-that-new-julia-programmers-tend-to-abuse-type-annotations/108465/62 "2024-01-10T15:58:50Z")

</div>

Mmmmmm, I don’t think so “strait”,  
For example in some cases I work with “Natural” numeric data, so for the definition of the functions I use arguments typed with “Unsigned” Abstract types, with this I design functions that are valid for all the “Concrete SubTypes” (“UInt…” ) in which I am interested. But I take care that all intermediate operations are performed on the Concrete Subtype of the data passed as argument.  
I check that there are no “Overflows” because Julia does not warn.  
The habit of declaring the function argument with an Abstract Type such that it sufficiently captures the different concrete subtypes that I am going to use into the function, gives me trust and good results.

```julia
 function consulta(m::Unsigned, Db7::IdDict)
        mtype=typeof(m)
        Db7keytype=Base.keytype(Db7)
        ..............

```

---

<div class="post-metadata">

**Author:** ![yvikhlya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yvikhlya/32/3753_2.png) [@yvikhlya](https://discourse.julialang.org/u/yvikhlya)\
**Post date:** [January 10, 2024, 5:06pm UTC](https://discourse.julialang.org/t/how-is-it-that-new-julia-programmers-tend-to-abuse-type-annotations/108465/64 "2024-01-10T17:06:14Z")

</div>

I put type annotations on function parameters, like `AbstractString`, `AbstractArray`, etc just as a hint (for myself) on what type does this function expect. Trying not to restrict it too much at same time. Probably, a proper doc string would be a better approach.

[Previous page](https://discourse.julialang.org/t/how-is-it-that-new-julia-programmers-tend-to-abuse-type-annotations/108465.md?page=2)

[Next page](https://discourse.julialang.org/t/how-is-it-that-new-julia-programmers-tend-to-abuse-type-annotations/108465.md?page=4)
