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 method signature / argument validation #548

Merged
merged 16 commits into from
Jan 31, 2014

Conversation

myronmarston
Copy link
Member

This is some further changes on top of #543.

I've done a bit of refactoring, renamed some things to reflect new realities (e.g. it's not just an arity check anymore), and fixed a couple of bugs I noticed when playing around with kw args.

Things I still want to consider doing:

  • There's an old arity check for and_yield that should be updated to use this.
  • I'd still like to consider raising an error message with all of the problems rather than just the first discovered problem. I think this is simpler with the current code structure than when we discussed it before. Nevermind, I've decided I'm ok with how it is.
  • The wrong number of args message may not match up with the numbers in the message that ruby generates, because we look at non-kw args and kw args separately for validation purposes but ruby kind of includes both in its concept of arity. The current message may be confusing and we should look more into it.

@xaviershay, the commits are pretty self-contained and since there are a bunch of changes made for different reasons (generally explained in the commit message), you may want to review this commit-by-commit.

It's a bit shorter but still obvious what it means.
- The signature of a method is a concept in its own
  right and deserves to be represented as its own object.
- This gives a clear delineaziation of responsibilities
  between extracting method signature info and verifying
  arguments against that info.
- The new `classify_parameters` approach should perform
  a bit better than what we had before; it iterates over
  `method.parameters` once total where as before it would
  iterate over it 3 times.
A last-arg hash should only be treated as kw args if the method has kw arg params.
- It was odd to have it memoize internally but accept an arg.
- Simpler just to call it once in `initialize`.
I find this:

foo(nil, :a => 1)

...to be much less noisy than this:

foo([nil, { :a => 1 }])

The first form more closely matches how people pass args
and kw args to methods, anyway.
To support `**kw_args`, `allowed_kw_args` will have
to act like an infinite set. Rather than making the
verifier (and any future clients) responsible for
dealing with this special case, it's simpler to move
the invalid/missing kw arg calculations into
`MethodSignature`, where it can handle that special
case internally.
BasicObject, for example, does not respond to it.
It's safer to use `SomeClass === obj` than `obj.is_a?(SomeClass)`.
Mutating could lead to surprises for the caller.
It's not a ruby error object, it's just the message,
so the new name is more accurate.
Arity is just the number of arguments but we check
more than that now, so it's good to reflect that.
@xaviershay
Copy link
Member

so beautiful

It's a description of the arity so let's call it that.
This clears the way to use a slightly different
signature implementation (e.g. for blocks).
I'm about to introduce a new `description` method
and having docstrings say "it is described precisely"
when not referring to the description is confusing.
@myronmarston
Copy link
Member Author

OK, I think this is ready to merge as far as I know. Any concerns, @xaviershay?

@xaviershay
Copy link
Member

LGTM, ship it.

myronmarston added a commit that referenced this pull request Jan 31, 2014
Improve method signature / argument validation
@myronmarston myronmarston merged commit dceb9e6 into master Jan 31, 2014
@JonRowe JonRowe deleted the improve-method-signature branch February 1, 2014 09:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants