-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Fix error message for calling record
macro with kwargs
#13367
Fix error message for calling record
macro with kwargs
#13367
Conversation
…of writing the name. Clarified error message.
src/macros.cr
Outdated
record #{name}, #{(properties + kwargs.map{|name, value| "#{name} : #{value}"}).join(", ").id} | ||
" unless kwargs.empty? %} |
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.
The trailing whitespace will still show up:
record #{name}, #{(properties + kwargs.map{|name, value| "#{name} : #{value}"}).join(", ").id} | |
" unless kwargs.empty? %} | |
record #{name}, #{(properties + kwargs.map { |name, value| "#{name} : #{value}" }).join(", ").id}" unless kwargs.empty? %} |
This is not unique to this PR, but I wonder why the compiler inserts a newline right after the first line of the error message itself |
This current solution does not work with Strings. I am not sure why. Any ideas? |
@a-alhusaini FWIW both of those examples produce an undefined constant error. I imagine it's just because |
Such an argument mismatch will always be possible as long as there is a positional parameter for the record's name, and Crystal does not have purely positional parameters. There are 2 possible solutions: (the second is probably better here) # uglier, but ensures no mismatch can ever happen
macro record(*name_and_properties, **kwargs)
{% name = name_and_properties[0] %}
{% properties = name_and_properties[1...name_and_properties.size] %}
end
# names containing `__` are reserved by Crystal, so technically
# no user code should ever use a named argument for `__name`
macro record(__name name, *properties, **kwargs)
end |
Co-authored-by: Sijawusz Pur Rahnama <sija@sija.pl>
record
macro with kwargs
Thanks to straight-shoota, hertdevil, blacksmoke, and devonte's thoughts and ideas we now get this error message when a named arg is provided to
record
The error is a bit long but I believe it is better than just telling the user that record does not accept named arguments. The space costs are more than made up for by the clarity of the message.