R:超载原始的非生成功能

发布于 2025-01-21 11:07:05 字数 551 浏览 1 评论 0原文

我想找到最小的黑客,以便能够说模块:: obj模块不是软件包,而是列表或环境。

经过一番挖掘,我看到了新用例的以下作用,但是打破了本机:

module = structure(list(f = \(x) x + 1), class = "module_cls")
`::` = function(mod, key) UseMethod("::")
`::.default` = function(mod, key) .Primitive("::")
`::.module_cls` = function(mod, key) mod[[as.character(substitute(key))]]

module::f(1)    # works!
base::sum(1, 1) # Error in base::sum : object 'base' not found

问题似乎是在default方法的定义中 或任何不module_cls的内容如何被划分为默认

I'd like to find the minimal hack to be able to say module::obj when module is not a package but a list or environment.

After some digging, I see the following works for the new use case, but breaks the native one:

module = structure(list(f = \(x) x + 1), class = "module_cls")
`::` = function(mod, key) UseMethod("::")
`::.default` = function(mod, key) .Primitive("::")
`::.module_cls` = function(mod, key) mod[[as.character(substitute(key))]]

module::f(1)    # works!
base::sum(1, 1) # Error in base::sum : object 'base' not found

The problem seems to be either in the definition of the default method
or how anything that is not module_cls is depatched to default.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文