| 1 |
#' Strict lists |
| 2 |
#' |
| 3 |
#' A strict list is essentially a normal [list()] but it does not |
| 4 |
#' allow partial matching with `$`. |
| 5 |
#' |
| 6 |
#' To me, partial matching is often more annoying and surprising than |
| 7 |
#' convenient. It can lead to bugs that are very hard to discover, and I have |
| 8 |
#' been bitten by it many times. When I write `x$name`, I always mean |
| 9 |
#' precisely `name`. You should use a modern code editor to autocomplete |
| 10 |
#' the `name` if it is too long to type, instead of using partial names. |
| 11 |
#' @param ... Objects (list elements), possibly named. Ignored in the |
| 12 |
#' `print()` method. |
| 13 |
#' @export |
| 14 |
#' @return Both `strict_list()` and `as_strict_list()` return a list |
| 15 |
#' with the class `xfun_strict_list`. Whereas `as_strict_list()` |
| 16 |
#' attempts to coerce its argument `x` to a list if necessary, |
| 17 |
#' `strict_list()` just wraps its argument `...` in a list, i.e., it |
| 18 |
#' will add another list level regardless if `...` already is of type |
| 19 |
#' list. |
| 20 |
#' @examples library(xfun) |
| 21 |
#' (z = strict_list(aaa = 'I am aaa', b = 1:5)) |
| 22 |
#' z$a # NULL! |
| 23 |
#' z$aaa # I am aaa |
| 24 |
#' z$b |
| 25 |
#' z$c = 'create a new element' |
| 26 |
#' |
| 27 |
#' z2 = unclass(z) # a normal list |
| 28 |
#' z2$a # partial matching |
| 29 |
#' |
| 30 |
#' z3 = as_strict_list(z2) # a strict list again |
| 31 |
#' z3$a # NULL again! |
| 32 |
strict_list = function(...) { |
| 33 |
as_strict_list(list(...)) |
| 34 |
} |
| 35 |
|
| 36 |
# https://twitter.com/xieyihui/status/782462926862954496 |
| 37 |
|
| 38 |
#' @param x For `as_strict_list()`, the object to be coerced to a strict |
| 39 |
#' list. |
| 40 |
#' |
| 41 |
#' For `print()`, a strict list. |
| 42 |
#' @rdname strict_list |
| 43 |
#' @export |
| 44 |
as_strict_list = function(x) { |
| 45 |
structure(as.list(x), class = 'xfun_strict_list') |
| 46 |
} |
| 47 |
|
| 48 |
#' @param name The name (a character string) of the list element. |
| 49 |
#' @rdname strict_list |
| 50 |
#' @export |
| 51 |
`$.xfun_strict_list` = function(x, name) x[[name]] |
| 52 |
|
| 53 |
#' @rdname strict_list |
| 54 |
#' @export |
| 55 |
print.xfun_strict_list = function(x, ...) { |
| 56 |
print(unclass(x)) |
| 57 |
} |
| 58 |
|
| 59 |
#' Print a character vector in its raw form |
| 60 |
#' |
| 61 |
#' The function `raw_string()` assigns the class `xfun_raw_string` to |
| 62 |
#' the character vector, and the corresponding printing function |
| 63 |
#' `print.xfun_raw_string()` uses `cat(x, sep = '\n')` to write the |
| 64 |
#' character vector to the console, which will suppress the leading indices |
| 65 |
#' (such as `[1]`) and double quotes, and it may be easier to read the |
| 66 |
#' characters in the raw form (especially when there are escape sequences). |
| 67 |
#' @param x For `raw_string()`, a character vector. For the print method, |
| 68 |
#' the `raw_string()` object. |
| 69 |
#' @param ... Other attributes for `x`. |
| 70 |
#' @export |
| 71 |
#' @examples library(xfun) |
| 72 |
#' raw_string(head(LETTERS)) |
| 73 |
#' raw_string(c('a "b"', 'hello\tworld!')) |
| 74 |
raw_string = function(x, ...) { |
| 75 |
if (is.null(x)) x = as.character(x) |
| 76 |
class(x) = c('xfun_raw_string', class(x)) |
| 77 |
structure(x, ...) |
| 78 |
} |
| 79 |
|
| 80 |
#' @param ... Other arguments (currently ignored). |
| 81 |
#' @rdname raw_string |
| 82 |
#' @export |
| 83 |
print.xfun_raw_string = function(x, ...) { |
| 84 |
if (length(x)) cat(x, sep = '\n') |
| 85 |
invisible(x) |
| 86 |
} |
| 87 |
|
| 88 |
# provide default chunk options to litedown |
| 89 |
#' @export |
| 90 |
record_print.xfun_raw_string = function(x, ...) { |
| 91 |
cls = intersect(c(class(x), 'record_output'), .record_classes)[1] |
| 92 |
structure(x, class = cls, opts = list(comment = '', attr = attr2(x, 'lang'))) |
| 93 |
} |