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

Support defmethod argcount validation #130

Merged
merged 6 commits into from
Sep 9, 2022

Conversation

camsaul
Copy link
Owner

@camsaul camsaul commented Sep 9, 2022

Resolves #59

This is implemented as a separate thing you have to opt-in to rather than using :arglists directly so we don't blow up in cases where people are using a human-friendly arglist like [x y? & {:as options]].

:defmethod-arities

A set of allowed/required arities that defmethod forms are allowed to have. defmethod forms must have arities that
match all of the specified :defmethod-arities, and all of its arities must be allowed by :defmethod-arities:

(m/defmulti ^:private mf
  {:arglists '([x]), :defmethod-arities #{1}}
  keyword)

(m/defmethod mf :x [x] x)
;; => ok

(m/defmethod mf :x ([x] x) ([x y] x y))
;; => error: {:arities {:disallowed #{2}}}

(m/defmethod mf :x [x y] x y)
;; => error: {:arities {:required #{1}}}


(m/defmethod mf :x [x y] x)
;; => error: {:arities {:required #{1 [:>= 3]}, :disallowed #{2}}}

:defmethod-arities must be a set of either integers or [:> n] forms to represent arities with & rest
arguments, e.g. [:>= 3] to mean an arity of three or-more arguments:

;; methods must both a 1-arity and a 3+-arity
(m/defmulti ^:private mf
  {:arglists '([x] [x y z & more]), :defmethod-arities #{1 [:>= 3]}}
  keyword)

(m/defmethod mf :x ([x] x) ([x y z & more] x))
;; => ok

When rest-argument arities are used, Methodical is smart enough to allow them when appropriate even if they do not
specifically match an arity specified in :defmethod-arities:

(m/defmulti ^:private mf
  {:arglists '([x y z & more]), :defmethod-arities #{[:>= 3]}}
  keyword)

(m/defmethod mf :x
  ([a b c] x)
  ([a b c d] x)
  ([a b c d & more] x))
;; => ok, because everything required by [:>= 3] is covered, and everything present is allowed by [:>= 3]

@codecov
Copy link

codecov bot commented Sep 9, 2022

Codecov Report

Base: 86.76% // Head: 87.09% // Increases project coverage by +0.32% 🎉

Coverage data is based on head (59963d9) compared to base (47381ad).
Patch coverage: 95.65% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #130      +/-   ##
==========================================
+ Coverage   86.76%   87.09%   +0.32%     
==========================================
  Files          21       22       +1     
  Lines        1194     1240      +46     
  Branches       66       68       +2     
==========================================
+ Hits         1036     1080      +44     
  Misses         92       92              
- Partials       66       68       +2     
Impacted Files Coverage Δ
src/methodical/macros/validate_arities.clj 95.65% <95.65%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@camsaul camsaul merged commit c223cab into master Sep 9, 2022
@camsaul camsaul deleted the support-defmethod-argcount-validation branch September 9, 2022 23:51
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.

defmethod should throw Exception if arg count doesn't match ^:arglists metadata from defmulti definition
1 participant