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

Improve the error message when the wrong number of arguments are provided #212

Merged

Conversation

metade
Copy link

@metade metade commented Jul 23, 2024

Currently when a user provides the wrong number of arguments, they receive a message saying #{function_name} is not implemented..

This message is confusing because it makes the user feel like they typed in the wrong function name, when in reality they just provided the wrong number of arguments.

Instead, a message such as wrong number of arguments to #{function_name} is more helpful to the user.

This is a first draft at a fix - feedback welcome!

Thins I'm not sure about:

  • the error message itself
  • what's happening with v2? I implemented the change in both but it's not clear to me if it's being tested.
  • is this the best way to check if a method exists regardless of the number of arguments? Enum.any?(0..20, fn arity -> Kernel.function_exported?(module, function_name, arity) end)
  • is there a way to share the functionality so that the wrong_arity_but_function_exists? isn't duplicated

Copy link
Contributor

@smn smn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I'm late to this PR as I was on leave.

First of all, thanks for your contribution! We'd love to know more about how you're using this library and if you're finding it useful.

To answer your questions about v2: it is on hold and potentially to be removed entirely. It was an attempt at a re-implementation to address some issues but we're not pursuing those at the moment. It's not receiving any active development, feel free to remove any of the changes you made to v2 (and reminding me that we need to mothball it).

On the function_exported?, let's try the Module.__info__() approach and see if that's a little less brittle?

# Otherwise fail
true ->
{:error, "#{function_name} is not implemented."}
end
end

defp wrong_arity_but_function_exists?(module, function_name)
when is_atom(module) and is_atom(function_name) do
Enum.any?(0..20, fn arity -> Kernel.function_exported?(module, function_name, arity) end)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the pragmatism here and I've not actually done this myself before but perhaps Module.__info__()[:functions] is a way to do this? The docs suggest that returns a list of functions and their arities so maybe that's worth cross referencing?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's much better, thank you!

TBH the Enum.any?(0..20, ... was a ChatGPT suggestion - I'm still an Elixir newbie so great to learn this!

…ided

Currently when a user provides the wrong number of arguments, they receive a
message saying `echo is not implemented.`.

This message is confusing because it makes the user feel like they typed in the
wrong function name, when in reality they just provided the wrong number of
arguments.

This is a first draft at a fix - feedback welcome!
@metade metade force-pushed the better-error-for-wrong-number-of-arguments branch from ec616d8 to 74ae0ab Compare August 28, 2024 07:10
@metade
Copy link
Author

metade commented Aug 28, 2024

Thank you for the feedback! I hope you had some lovely time off on your leave!

We'd love to know more about how you're using this library and if you're finding it useful.

I'm actually @lorenzosinisi's colleague, we're building a back office tool where expression allows non-developers to transform data.

It's not receiving any active development, feel free to remove any of the changes you made to v2

Great, that makes sense - I removed those changes.

On the function_exported?, let's try the Module.info() approach and see if that's a little less brittle?

I have done this - looking forwards to the next review.

@metade metade requested a review from smn August 28, 2024 07:37
Copy link
Contributor

CLA Assistant Lite bot:
Thank you for your submission, we really appreciate it. Like many open-source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution. You can sign the CLA by just posting a Pull Request Comment same as the below format.


I have read the CLA Document and I hereby sign the CLA


1 out of 2 committers have signed the CLA.
@smn
@patrick Sinclair
Patrick Sinclair seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You can retrigger this bot by commenting recheck in this Pull Request

@smn smn merged commit e01b077 into turnhub:develop Sep 26, 2024
1 of 2 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Sep 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants