# Finding which var is an invalid type when initialising a struct

**URL:** <https://discourse.julialang.org/t/finding-which-var-is-an-invalid-type-when-initialising-a-struct/78734>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 30, 2022, 11:42am UTC](https://discourse.julialang.org/t/finding-which-var-is-an-invalid-type-when-initialising-a-struct/78734 "2022-03-30T11:42:11Z")\
**Posts on this page:** 2\
**Page:** 2

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [March 30, 2022, 3:34pm UTC](https://discourse.julialang.org/t/finding-which-var-is-an-invalid-type-when-initialising-a-struct/78734/21 "2022-03-30T15:34:11Z")

</div>

> [@Henrique\_Becker](#):
>
> - The exception type [`ArgumentError` description](https://docs.julialang.org/en/v1/base/base/#Core.ArgumentError) matches the case.
> - The exception type `ArgumentError` is already used pervasively in `Base` : [Search · ArgumentError · GitHub](https://github.com/JuliaLang/julia/search?q=ArgumentError)

Note that (I haven’t checked, but I’m pretty sure) all of those cases in `Base` are thrown when it’s impossible to let dispatch handle it, i.e. when the argument provided is a valid instance of the type it is, but it’s nevertheless not suitable for the called function. A good example is [this](https://github.com/JuliaLang/julia/blob/9739f504e584eefe3cdd0e1f1255893dfe3e39d9/base/parse.jl#L42), where a valid `Int` has been passed but it’s not in the range of acceptable values. This is different from the cases presented in this thread, where it’s a specific _Type_ that isn’t wanted, for which `MethodError` is more applicable (though throwing it yourself is bad, which is why I suggested `ArgumentError` above instead).

Maybe the docstring of `ArgumentError` could be amended to clarify this nuance - I’ll see if I can find the time to make a PR tomorrow.

> [@Henrique\_Becker](#):
>
> “a MethodError could not have been thrown, there is at least one method that matches the arguments that is this generic one”.

I do agree that explicitly throwing `MethodError` ala `throw(MethodError("..."))` is very bad form and the (I think) only place in `Base` where this was the case has been removed since. That’s also not what I’m arguing for here though - I’m proposing to annotate the types in the constructor, which will result in a `MethodError` all on its own.

Regardless of throwing `MethodError` explicitly or not, in either case the stacktrace will have the offending call in it, and following it is very much standard debugging practice.

---

<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:** [March 31, 2022, 12:16am UTC](https://discourse.julialang.org/t/finding-which-var-is-an-invalid-type-when-initialising-a-struct/78734/22 "2022-03-31T00:16:01Z")

</div>

While I see now that we agree on more than it seemed to me reading your last post, I yet disagree on one point: passing some type of object as an argument to the constructor can be a common error, and you may want to have a method that allow such input just to spew an `ArgumentError` giving specific guidance on that (or you may want to block some specific case to avoid confusion, like allowing `Number`s but not `Bool` which is a `Number`). But maybe, the right way to deal with that is the [Base.Experimental.register\_error\_hint](https://docs.julialang.org/en/v1/base/base/#Base.Experimental.register_error_hint) I mentioned before.

[Previous page](https://discourse.julialang.org/t/finding-which-var-is-an-invalid-type-when-initialising-a-struct/78734.md?page=1)
