# Match a type or an instance of it

**URL:** <https://discourse.julialang.org/t/match-a-type-or-an-instance-of-it/43500>\
**Category:** New to Julia\
**Created:** [July 22, 2020, 3:15pm UTC](https://discourse.julialang.org/t/match-a-type-or-an-instance-of-it/43500 "2020-07-22T15:15:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [July 22, 2020, 3:15pm UTC](https://discourse.julialang.org/t/match-a-type-or-an-instance-of-it/43500/1 "2020-07-22T15:15:38Z")

</div>

I want to write a function which can dispatch on the type as well as on an instance of it.

I tried the following:

```julia
struct mytype end
f(Union{Type{mytype},mytype}) = 42
f(mytype)
f(mytype())

```

Is there a better way of doing this?

---

<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:** [July 22, 2020, 3:22pm UTC](https://discourse.julialang.org/t/match-a-type-or-an-instance-of-it/43500/2 "2020-07-22T15:22:28Z")

</div>

I don’t see how it could get any simpler.

Note, however, that this is generally [discouraged](https://docs.julialang.org/en/v1/manual/style-guide/#Avoid-confusion-about-whether-something-is-an-instance-or-a-type-1) style.

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [July 22, 2020, 3:24pm UTC](https://discourse.julialang.org/t/match-a-type-or-an-instance-of-it/43500/3 "2020-07-22T15:24:06Z")

</div>

Nice link. Thank you!
