Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upstrictly() #275
strictly() #275
Comments
|
Keeping with the principle of separating concerns, it would be handy if |
|
It seems https://cran.r-project.org/web/packages/ensurer/vignettes/ensurer.html |
|
@smbache I was unaware of What I understand of In another sense, however, I think the value of Have I understood the role and function of |
|
In addition to a
Both |
|
This is a big problem so I think it's out of scope for purrr. Also see https://github.com/jimhester/types |
|
@hadley I guess I need to do my homework! Didn't know about these packages, either. Thanks. Both Types for R, and the related package Declarative function argument check, tackle the larger problem. The second one especially is quite close in implementation to what I've suggested, but uses roxygen comments, rather than formulae, to specify checks. Relying on a comments-based mechanism is not as flexible as using formulae, à la lazyeval.
But you are right. The basic problem is big and nontrivial, and I do see the rationale behind leaving |
|
@hadley You're right, |
A common form of boilerplate code at the top of functions is argument checking: You make some checks on the arguments, signal a condition if any show-stopping checks fail, then move on to the meat of the function if everything is good. The problem with this approach is that it can clutter up the main work of a function with admin; it spoils the "fun" of a function with the inconvenience of a security check.
A function
strictly(), as a cohort ofsafely()andpossibly(), would separate these concerns, functionally. It could have the signaturewhere
.fis an interpreted function andcondis a condition object (if notNULL). The main part is..., which consists of two-sided formulasp ~ mthat specify the argument checks for.f:pis an expression that is a call to a predicate function (the "check")mis an expression whose evaluation can be coerced to a string (the "failure message" if the check fails)The value of
strictly(.f, ...)would be a function that applies.f"strictly."Example:
(While the error messages could be more informative, the basic utility is clear.)
Here's a basic implementation, with a modicum of metaprogramming:
Alternatively, one could implement
strictly()as a function-composition. However, the above implementation has the advantage of preserving the argument signature of.f, as well as its source code and environment.Would
strictly()be a meaningful addition to purrr?