【问题标题】:When does the object returned by invisible() cease to be invisible?invisible() 返回的对象何时不再不可见?
【发布时间】:2015-09-14 20:59:55
【问题描述】:

?invisible

返回一个(暂时)不可见的对象副本。

那个括号暗示隐形不会永远持续下去,但我找不到任何可以解释它何时消失的东西。我特别想知道像这样的构造(来自this old answer of mine):

printf <- function(...) invisible(print(sprintf(...)))

外部invisible可能是不必要的(因为print 已经将其返回值标记为不可见)。 withVisible() 报告说这个函数的返回值是不可见的,但我不知道这是否由语言保证,或者只是它在当前实现中的工作方式。

【问题讨论】:

  • 它看起来 C 代码只是返回了它的参数。所以,暂时的,它可能只是意味着从 invisible 调用中返回的任何内容都不会打印,但是像 printf &lt;- function(...) +invisible(1); printf(1) 这样的东西仍然会打印 1,而 printf &lt;- function(...) invisible(1); printf(1) 不会

标签: r language-lawyer


【解决方案1】:

通过反复试验:

# invisible
withVisible(invisible())$visible
[1] FALSE

### passing the invisible value through a function seems to
# preserve the invisibility
withVisible(identity(invisible()))$visible
[1] FALSE

# the <- operator just returns its arguments, so it confirms the above
withVisible(i <- invisible())$visible
[1] FALSE
# but the assigned value is no longer invisible
withVisible(i)$visible
[1] TRUE

### passing an invisible value as argument keeps the invisibility
f <- function(x) withVisible(x)$visible
f(1)
[1] TRUE
f(invisible(1))
[1] FALSE

### every other operation seems to cancel the invisibility.
# e.g. assigning an invisible value cancels the it
i <- invisible()
withVisible(i)$visible
[1] TRUE

withVisible(invisible(1) + 1)$visible
[1] TRUE

【讨论】:

  • 这是有用的信息,但我真的希望从 语言规范 中得到答案(在 R 的范围内),而不是观察到的实现行为。换句话说,我们可以看到它做了什么,但它是应该做的吗,它会保持这种状态吗?
猜你喜欢
  • 2015-10-19
  • 1970-01-01
  • 2011-02-04
  • 1970-01-01
  • 1970-01-01
  • 2018-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多