|
3 | 3 | #' Compile an R function. |
4 | 4 | #' |
5 | 5 | #' @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. |
7 | 10 | #' |
8 | 11 | #' @details |
9 | 12 | #' |
|
12 | 15 | #' The shape and mode of all function arguments must be declared. Local and |
13 | 16 | #' return variables may optionally also be declared. |
14 | 17 | #' |
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: |
17 | 21 | #' |
18 | 22 | #' ```r |
19 | 23 | #' declare(type(x = double(NA))) # x is a 1-d double vector of any length |
|
64 | 68 | #' |
65 | 69 | #' ## Return values |
66 | 70 | #' |
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. |
72 | 76 | #' |
73 | 77 | #' ```r |
74 | 78 | #' # Will fail to compile: |
|
82 | 86 | #' }) |
83 | 87 | #' ``` |
84 | 88 | #' |
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: |
87 | 91 | #' |
88 | 92 | #' ```r |
89 | 93 | #' # Succeeds: |
@@ -145,6 +149,12 @@ quick <- function(fun, name = NULL) { |
145 | 149 |
|
146 | 150 | pkgname <- parent.pkg() |
147 | 151 | 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 | + } |
148 | 158 | # we are in a package - but outside a quickr::compile_package() call. |
149 | 159 | return(create_quick_closure(name, fun)) |
150 | 160 | } |
|
0 commit comments