The representation of an empty argument in a “call”

懵懂的女人 提交于 2020-08-17 11:55:32

问题


What kind of animal is an empty argument? Consider the following piece of code.

> f <- function(...) match.call()
> l <- as.list(f(,3))
> l
[[1]]
f

[[2]]


[[3]]
[1] 3

> typeof(l[[2]])
[1] "symbol"

> identical(l[[2]],``)
Error: attempt to use zero-length variable name

> as.character(l[[2]])
[1] ""

What sorcery is that?


回答1:


If looks like l[[2]]'s value is a special object: the empty symbol. Some facts about the empty symbol (speculated based on casual experimentation).

(1) The empty symbol is a symbol:

> is.symbol(l[[2]])
[1] TRUE

(2) The empty symbol cannot be created explicitly:

> as.name("")
Error in as.name("") : attempt to use zero-length variable name
> ``
Error: attempt to use zero-length variable name
> quote()
Error in quote() : 0 arguments passed to 'quote' which requires 1

(3) The only way to create an empty symbol is by quote-ing a function call and passing an empty argument, e.g.:

> c <- quote(f(,0))
> d <- quote(f(x, n=, 0))

The empty symbol can now be accessed thus: c[[2]], d$n, as.list(d)[[3]]

(4) The empty value is unique and does not equal any other symbol:

> identical(c[[2]], d$n)
[1] TRUE

> identical(c[[2]], `a`)
[1] FALSE

(5) The empty symbol cannot be assigned to a standalone variable. If assigned to such a variable, the variable is actually assigned the "missing argument" value:

> x <- d$n
> missing(x)
[1] TRUE
> missing(d$n)
Error in missing(d$n) : invalid use of 'missing'

However, the empty symbol can be assigned to a list slot:

> p <- list(`a`, k=`b`)
> p[[1]] <- d$n
> p$k <- c[[2]]

> identical(p[[1]], d$n)
[1] TRUE

> identical(p[[1]], p[[2]])
[1] TRUE

And it can be passed as a function argument:

> h <- function(x) identical("", d$n)
> h(c[[2]])
[1] TRUE

When passed as a function argument, the argument is not considered to be missing:

> h2 <- function(x) missing(x)
> h2(c[[2]])
[1] FALSE

But compare

> h2(x)
[1] TRUE

(6) The character value of the empty symbol is the empty string:

> identical("", as.character(d$n))
[1] TRUE

(7) There is no primitive function that checks whether a value is the empty symbol. To check whether a value is the empty symbol, its character representation needs to be compared to the empty string:

> is.symbol(d$n) && identical("", as.character(d$n))
[1] TRUE

Appendix

Here are a number of function definitions to facilitate the creation and identification of empty symbols and "missing argument" objects.

> isEmptySymbol
function(x) is.symbol(x) && identical("", as.character(x))

> EmptySymbol
function() (quote(f(,)))[[2]]

> isMissingArgObject
function(x) {
if ("x" %in% names(match.call())) return (missing(x))
stop("An argument must be supplied.") }

> MissingArgObject
function() quote(expr=)


来源:https://stackoverflow.com/questions/20904827/the-representation-of-an-empty-argument-in-a-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!