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

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.

1 Like