-
Notifications
You must be signed in to change notification settings - Fork 3.4k
raise compile error when inspect protocol options don't match struct #11801
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
raise compile error when inspect protocol options don't match struct #11801
Conversation
lib/elixir/lib/inspect.ex
Outdated
defp validate_option(option_list, fields, module, caller) do | ||
if not Enum.empty?(option_list -- fields) do | ||
description = "When deriving inspect protocol of #{module}, values must match struct fields" | ||
raise CompileError, file: caller.file, line: caller.line, description: description |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be a regular ArgumentError :) CompileError is typically only raise from within the compiler.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would also be nice to show which fields do not match. Perhaps convert the if
to a case and pattern match accordingly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated dc203bf
lib/elixir/lib/inspect.ex
Outdated
@@ -478,6 +481,15 @@ defimpl Inspect, for: Any do | |||
end | |||
end | |||
|
|||
defp validate_option(option_list, fields, module, caller) do | |||
if not Enum.empty?(option_list -- fields) do | |||
description = "When deriving inspect protocol of #{module}, values must match struct fields" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
description = "When deriving inspect protocol of #{module}, values must match struct fields" | |
description = "unknown fields #{inspect(...)} given when deriving the Inspect protocol for #{inspect(module)}. :only and :except values must match struct fields" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Originally i had something like this, but the problem is that inspect/1
is not yet available. I looked at some of the erlang :io_lib
libraries and at :erlang.display/1
but those weren't exactly right. I'll take another stab at it using Enum.join/2
which will trim off the :
s
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, we unimport inspect to avoid conflcit. You can use Kernel.inspect(module)
instead. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, we unimport inspect to avoid conflcit. You can use
Kernel.inspect(module)
instead. :)
Thanks! it's wild how tricky it gets when i think I'm missing one function :)
updated: dc203bf
Thank you! I have dropped some comments which are mostly nitpicks around the error message! |
💚 💙 💜 💛 ❤️ |
No description provided.