Skip to content

Commit 13a3d86

Browse files
authored
Merge pull request #47 from t-kalinowski/pass-name-as-positional-arg
Improve docs and error if using an anonymous funcs in an R package.
2 parents 2ac107d + 11bdd31 commit 13a3d86

3 files changed

Lines changed: 40 additions & 20 deletions

File tree

R/quick.R

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
#' Compile an R function.
44
#'
55
#' @param fun An R function
6-
#' @param name Optional string, name to use for the function.
6+
#' @param name String, name to use for the function. This is optional in
7+
#' regular usage but required in an R package. As a convenience, arguments
8+
#' `fun` and `name` can also be supplied as positional arguments to `quick` with
9+
#' `name` in the first position.
710
#'
811
#' @details
912
#'
@@ -12,8 +15,9 @@
1215
#' The shape and mode of all function arguments must be declared. Local and
1316
#' return variables may optionally also be declared.
1417
#'
15-
#' `declare(type())` also has support for declaring size constraints, or size
16-
#' relationships between variables. Here are some examples of declare calls:
18+
#' `declare(type())` also has support for declaring size constraints, or
19+
#' size relationships between variables. Here are some examples of declare
20+
#' calls:
1721
#'
1822
#' ```r
1923
#' declare(type(x = double(NA))) # x is a 1-d double vector of any length
@@ -64,11 +68,11 @@
6468
#'
6569
#' ## Return values
6670
#'
67-
#' The shape and type of a function return value must be known at compile time.
68-
#' In most situations, this will be automatically inferred by `quick()`. However,
69-
#' if the output is dynamic, then you may need to provide a hint.
70-
#' For example, returning the result of `seq()` will fail because the output shape
71-
#' cannot be inferred.
71+
#' The shape and type of a function return value must be known at compile
72+
#' time. In most situations, this will be automatically inferred by
73+
#' `quick()`. However, if the output is dynamic, then you may need to
74+
#' provide a hint. For example, returning the result of `seq()` will fail
75+
#' because the output shape cannot be inferred.
7276
#'
7377
#' ```r
7478
#' # Will fail to compile:
@@ -82,8 +86,8 @@
8286
#' })
8387
#' ```
8488
#'
85-
#' However, if the output size can be declared as a dynamic expression using other
86-
#' values known at runtime, compilation will succeed:
89+
#' However, if the output size can be declared as a dynamic expression using
90+
#' other values known at runtime, compilation will succeed:
8791
#'
8892
#' ```r
8993
#' # Succeeds:
@@ -145,6 +149,12 @@ quick <- function(fun, name = NULL) {
145149

146150
pkgname <- parent.pkg()
147151
if (!is.null(pkgname) && pkgname != "quickr") {
152+
if (startsWith(name, 'anonymous_quick_function_')) {
153+
stop(
154+
'When used in an R package, you must provide a unique `name` to every `quick()` call.\n',
155+
'For example: `my_fun <- quick("my_fun", function(x) ....)'
156+
)
157+
}
148158
# we are in a package - but outside a quickr::compile_package() call.
149159
return(create_quick_closure(name, fun))
150160
}

README.Rmd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,12 @@ plot(timings) + bench::scale_x_bench_time(base = NULL)
364364
When called in a package, `quick()` will pre-compile the quick functions and place them in the `./src` directory.
365365
Run `devtools::load_all()` or `quickr::compile_package()` to ensure that the generated files in `./src` and `./R` are in sync with each other.
366366

367+
In a package, you must provide a function name to `quick()`. For example:
368+
369+
```r
370+
my_fun <- quick(name = "my_fun", function(x) ....)
371+
```
372+
367373
## Installation
368374

369375
You can install quickr from CRAN with:

man/quick.Rd

Lines changed: 14 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)