# How to input types for functions in a more compact way?

**URL:** <https://discourse.julialang.org/t/how-to-input-types-for-functions-in-a-more-compact-way/94221>\
**Category:** General Usage\
**Created:** [February 7, 2023, 4:39pm UTC](https://discourse.julialang.org/t/how-to-input-types-for-functions-in-a-more-compact-way/94221 "2023-02-07T16:39:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![josimar](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@josimar](https://discourse.julialang.org/u/josimar)\
**Post date:** [February 7, 2023, 4:39pm UTC](https://discourse.julialang.org/t/how-to-input-types-for-functions-in-a-more-compact-way/94221/1 "2023-02-07T16:39:39Z")

</div>

Hi,

I am trying to improve the way that I define the input types for my functions in Julia. I have a function that can take as input array values of type `Dates.Date`, `Dates.DateTime` or `StepRange{Dates.Date, Dates.Day}`. See below the example.

Do you know of a more compact way of defining the input types for this function? Could you please also point it out to the source of where you learned this information?

Thanks!

```julia
myFunction(x::Union{Array{Dates.Date}, Array{Dates.DateTime}, StepRange{Dates.Date, Dates.Day}})

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [February 7, 2023, 4:52pm UTC](https://discourse.julialang.org/t/how-to-input-types-for-functions-in-a-more-compact-way/94221/2 "2023-02-07T16:52:43Z")

</div>

One alternative is to define a type alias,

```julia
const AcceptedDateArrays = Union{
    Array{Dates.Date},
    Array{Dates.Day},
    StepRange{Dates.Date, Dates.Day}
}

myfunction(x::AcceptedDateArrays) = ...

```

that makes it easy to add/remove types later if you want.

Another (more common maybe) alternative is to just let it go, and use:

```julia
myfunction(x::AbstractArray) = ...

```

if an input that that is not adequate is passed on, an error will occur later inside the function. One advantage of this is that your function probably should work with more abstract arrays of the ones defined above, like slices of the arrays of dates, static arrays of dates, etc. Having a general interface like this one allows your function to accept those.

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [February 7, 2023, 5:05pm UTC](https://discourse.julialang.org/t/how-to-input-types-for-functions-in-a-more-compact-way/94221/3 "2023-02-07T17:05:32Z")

</div>

I’ve been using Julia for a few years and it never occured to me that I could use “,” to add multiple type signatures 😅

Thank you for asking!

---

<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:** [February 7, 2023, 8:15pm UTC](https://discourse.julialang.org/t/how-to-input-types-for-functions-in-a-more-compact-way/94221/4 "2023-02-07T20:15:46Z")

</div>

The collection of types seems oddly specific. Why not use something more abstract? For example

```julia
AbstractVector{<:TimeType} # or AbstractArray

```

?  
If you can use a general vector (or N-dim array?!) of times, as well as a range, why not this?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 7, 2023, 8:26pm UTC](https://discourse.julialang.org/t/how-to-input-types-for-functions-in-a-more-compact-way/94221/5 "2023-02-07T20:26:17Z")

</div>

> [@josimar](#):
>
> Do you know of a more compact way of defining the input types for this function?

one possibility is you don’t annotate the type at all.
