Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error hint for `setindex(::Type, ...) #53241

Closed
oscardssmith opened this issue Feb 7, 2024 · 4 comments
Closed

Add error hint for `setindex(::Type, ...) #53241

oscardssmith opened this issue Feb 7, 2024 · 4 comments
Labels
error messages Better, more actionable error messages good first issue Indicates a good issue for first-time contributors to Julia

Comments

@oscardssmith
Copy link
Member

The typical pattern that this would help is something like

d = Dict
d[1] = 2

which gives the only somewhat helpful error message ERROR: MethodError: no method matching setindex!(::Type{Dict}, ::Int64, ::Int64). Since Types are never indexable, it seems like it would be reasonable to give an error hint that links to some docs on the difference between a type and a type constructor.
See https://discourse.julialang.org/t/use-dict-to-store-tree-with-uninitialized-tree-nodes/109895 for an example of a user running into this.

@oscardssmith oscardssmith added good first issue Indicates a good issue for first-time contributors to Julia error messages Better, more actionable error messages labels Feb 7, 2024
@AbhinavMir
Copy link

Would something like this work? - ERROR: Cannot assign value to 'Dict' type directly. Use 'Dict()' to create an instance before assignment.

@oscardssmith
Copy link
Member Author

oscardssmith commented Feb 11, 2024

looks great! (although obviously this shouldn't be specifically for Dict but whatever the type name happens to be)

@AbhinavMir
Copy link

AbhinavMir commented Feb 11, 2024

Thanks! How far should it go? ERROR: Cannot assign value to '<TypeName>' directly. Initialize with '<TypeName>()' before assignment. seems generic enough. Also: Where in the source code is this error actually being triggered? I assume It's multiple methods forming the final string, but where would I usually start at?

Cheers

@vtjnash
Copy link
Member

vtjnash commented Feb 12, 2024

It could likely be added as an additional MethodError hint with register_error_hint (or it could be a method definition like setindex!(T::Type, val, inds...) = error("Cannot assign a value to $T directly. Use $T() ...") instead, but the former seems probably preferable?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
error messages Better, more actionable error messages good first issue Indicates a good issue for first-time contributors to Julia
Projects
None yet
Development

No branches or pull requests

3 participants