# Type annotation without a variable name in a function declaration

**URL:** <https://discourse.julialang.org/t/type-annotation-without-a-variable-name-in-a-function-declaration/101969>\
**Category:** New to Julia\
**Created:** [July 23, 2023, 5:18pm UTC](https://discourse.julialang.org/t/type-annotation-without-a-variable-name-in-a-function-declaration/101969 "2023-07-23T17:18:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![enderw88](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/enderw88/32/48997_2.png) [@enderw88](https://discourse.julialang.org/u/enderw88)\
**Post date:** [July 23, 2023, 5:18pm UTC](https://discourse.julialang.org/t/type-annotation-without-a-variable-name-in-a-function-declaration/101969/1 "2023-07-23T17:18:02Z")

</div>

I am picking up a stale package that reads a specific type of text file (IGC file if anyone out there flies gliders).

I have come across a number of function declarations like this:

```julia
function parse(::Type{IGCPressureAltitude}, s::String)
    return IGCPressureAltitude(parse(Int, s))
end

```

and

```julia
function parse(::Type{IGCGpsAltitude}, s::String)
    return IGCGpsAltitude(parse(Int, s))
end

```

`parse` has a parse definition for every kind of data line in the file format.

I think the only thing that does is support multiple dispatch by passing a raw type into the `parse` function. Am I missing anything?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [July 23, 2023, 5:20pm UTC](https://discourse.julialang.org/t/type-annotation-without-a-variable-name-in-a-function-declaration/101969/2 "2023-07-23T17:20:31Z")

</div>

No, I believe you are right. This way, one can write generic code that calls the `parse` function for a type that is easy to change.

---

<div class="post-metadata">

**Author:** ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)\
**Post date:** [July 23, 2023, 5:30pm UTC](https://discourse.julialang.org/t/type-annotation-without-a-variable-name-in-a-function-declaration/101969/3 "2023-07-23T17:30:01Z")

</div>

You can read more about this [here](https://docs.julialang.org/en/v1/manual/methods/#Trait-based-dispatch) - depending on the implementation, this can be part of the [Holy Trait](https://github.com/JuliaLang/julia/issues/2345) pattern.

---

<div class="post-metadata">

**Author:** ![enderw88](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/enderw88/32/48997_2.png) [@enderw88](https://discourse.julialang.org/u/enderw88)\
**Post date:** [July 23, 2023, 5:54pm UTC](https://discourse.julialang.org/t/type-annotation-without-a-variable-name-in-a-function-declaration/101969/4 "2023-07-23T17:54:06Z")

</div>

Perfect, thank you very much.

---

<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:** [July 23, 2023, 6:33pm UTC](https://discourse.julialang.org/t/type-annotation-without-a-variable-name-in-a-function-declaration/101969/5 "2023-07-23T18:33:33Z")

</div>

> [@enderw88](#):
>
> I think the only thing that does is support multiple dispatch by passing a raw type into the `parse` function. Am I missing anything?

You are right about this, but looking at your question title, “Type annotation without a variable name in a function declaration”, there is something more. You can also have something like

```julia
function foo(::Float64, y::String) 
   ... 

```

etc. That will dispatch on the type of the first input, but does not capture it in a variable.

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [July 23, 2023, 8:09pm UTC](https://discourse.julialang.org/t/type-annotation-without-a-variable-name-in-a-function-declaration/101969/6 "2023-07-23T20:09:48Z")

</div>

You can also use type unions to prevent code duplication. Your two methods could be replaced with a single method:

```julia
function parse(::Type{T}, s::String) where {
    T <: Union{IGCPressureAltitude, IGCGpsAltitude},
}
    return T(parse(Int, s))
end

```

Alternative notation:

```julia
const MyUnion = Union{IGCPressureAltitude, IGCGpsAltitude}

parse(::Type{T}, s::String) where {T<:MyUnion} = T(parse(Int, s))

```
