# How do I find if an object is a Struct, a Dictionary or a NamedTuple?

**URL:** <https://discourse.julialang.org/t/how-do-i-find-if-an-object-is-a-struct-a-dictionary-or-a-namedtuple/74364>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 10, 2022, 10:10pm UTC](https://discourse.julialang.org/t/how-do-i-find-if-an-object-is-a-struct-a-dictionary-or-a-namedtuple/74364 "2022-01-10T22:10:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Soldalma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/soldalma/32/29388_2.png) [@Soldalma](https://discourse.julialang.org/u/Soldalma)\
**Post date:** [January 10, 2022, 10:10pm UTC](https://discourse.julialang.org/t/how-do-i-find-if-an-object-is-a-struct-a-dictionary-or-a-namedtuple/74364/1 "2022-01-10T22:10:18Z")

</div>

I wrote simple functions that print structs, dictionaries and named tuples nicely. I would like to write a function that takes a vector and prints each element, calling one of these three functions if the element is a struct, a dictionary or a named tuple. Something like this:

```julia
function print_stuff(alist)
    for x in alist
        typx = typeof(x)
        if typx == Struct
            print_struct(x)
        elseif typx == Dictionary
            print_dict(x)
        elseif typx == NamedTuple
            print_named_tuple(x)
        else
            println(x)
        end
    end
end

```

Of course it does not work. But how can I implement something like this with Julia?

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [January 10, 2022, 10:21pm UTC](https://discourse.julialang.org/t/how-do-i-find-if-an-object-is-a-struct-a-dictionary-or-a-namedtuple/74364/2 "2022-01-10T22:21:39Z")

</div>

You can use `<:` to check if a type is a subtype of the abstract types `Dict` or `NamedTuple`. Alternatively, you use `isa` to check if an instance fits those types (in this case, you won’t need to use `typeof` first). As other commenters have noted, this type-checking also happens during method dispatch, and multimethods are much easier to maintain than an if statement.

Structs aren’t a particular type, so you’ll need something different to check: `isstructtype`. That method only works on types, not instances.

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [January 10, 2022, 10:22pm UTC](https://discourse.julialang.org/t/how-do-i-find-if-an-object-is-a-struct-a-dictionary-or-a-namedtuple/74364/3 "2022-01-10T22:22:46Z")

</div>

[https://docs.julialang.org/en/v1/manual/types/#man-custom-pretty-printing](https://docs.julialang.org/en/v1/manual/types/#man-custom-pretty-printing)

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [January 10, 2022, 10:27pm UTC](https://discourse.julialang.org/t/how-do-i-find-if-an-object-is-a-struct-a-dictionary-or-a-namedtuple/74364/4 "2022-01-10T22:27:39Z")

</div>

You can test `x isa AbstractDict` etc, or `typx <: NamedTuple` etc.

But what you will frequently see is that instead of making three functions with different names, just three methods of the same function, and then let dispatch sort them out:

```julia
my_print(x::AbstractDict) = ... # dictionary printing code
my_print(x::NamedTuple) = ...
my_print(x) = # struct printing code

print_stuff(alist) = foreach(my_print, alist)

```

A much more elaborate version of this scheme, mostly involving methods of `Base.show`, controls how printing at the REPL works. Unlike the `if` block, new methods for new types can be added.
