将符号暴露给 $ContextPath
有多种有用的Internal`
上下文函数,例如InheritedBlock
, Bag
和 StuffBag
等,以及许多有用的 Developer`
功能。
我希望公开这些符号的选择,以便可以在没有上下文名称的情况下清楚地寻址它们,但我不想通过添加 Internal`
和 Developer` 到
$ContextPath
。
我可以使用诸如 Bag = Internal`Bag
之类的代理符号,但这既不干净也不完全正确,因为它是一个引用,并且例如属性不是继承的。
有没有一种方法可以选择性地公开我想要的符号,而无需诉诸上面的麻烦?
There are a variety of Internal`
context functions that are useful, such as InheritedBlock
, Bag
and StuffBag
, etc., as well as many useful Developer`
functions.
I wish to expose a selection of these symbols such that they may be addressed plainly without the context name, but I do not want to expose all of them by adding Internal`
and Developer`
to $ContextPath
.
I could use a proxy symbol such as Bag = Internal`Bag
but this is neither clean nor fully correct, since it is a reference, and e.g. attributes are not inherited.
Is there a way to selectively expose the symbols I want, without resorting to the kluge above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我看来,这是一个非常深刻和有效的问题,从投票数来看,我并不是唯一一个这么想的人。如果我们在语言级别上完全支持此功能,这将提供一种全新的方式来操作范围和封装,并且 IMO 通常会允许更清晰的代码和更好的信息隐藏。这类似于 Python 中的
from module-name import name1,name2,...
。也许和我们许多人一样,我尝试过多种方法,但所有这些方法似乎都很脆弱且不完整。最坏的情况是对于包,对此我没有很好的解决方案。对于前端的交互工作,这里是一个可能可以接受的。首先定义一个通用宏来进行文字替换:
然后,创建一个包含您最喜欢的快捷方式的函数,例如:
您现在可以将代码包装在
shortcutF
中并开始使用短名称。到目前为止,这也适用于包,但您必须将所有代码(或至少包含快捷方式的代码)包装在shortcutF
中,这不是很方便。作为进一步方便的步骤,您可以将上述函数分配给$Pre
:以下是一些使用示例:
由于
With
是在幕后使用的,所以真正发生的情况是在执行代码之前,您的快捷符号将被完全限定的符号名称替换。这是我所能得到的,但这个功能似乎特别需要该语言的本地支持。
This is IMO a very deep and valid question, and judging by the vote count, I am not alone in thinking this. If we had this feature fully supported on the language level, this would give one a whole new way to manipulate the scoping and encapsulation, and would IMO often allow for a cleaner code and better information hiding. This would be similar to
from module-name import name1,name2,...
in Python.Perhaps as many of us, I have tried multiple approaches, but all of them seem fragile and incomplete. The worst case is for packages, for which I have no good solution. For the interactive work in the FrontEnd, here is one which might be acceptable. First define a general macro to make the literal substitutions:
Then, create a function which would incorporate your favorite shortcuts, for example:
You can now wrap your code in
shortcutF
and start using the short names. Up to now, this would also work for packages, but you will have to wrap all your code (or at least those pieces which contain short-cuts) inshortcutF
, which is not very convenient. As a further convenience step, you may assign the above function to$Pre
:Here are some examples of use:
Since
With
is used under the cover, what really happens is that your short-cut symbols get substituted by fully-qualified symbol names, before the code is executed.This is as far as I could get, but this feature seems to be particularly crying for a native support from the language.
Leonid 的答案的一个变体适用于早期阶段:
在笔记本中输入此内容后,输入
给我
while
gets
但
gets
但是它似乎只在笔记本界面中工作,而不是在命令行上使用数学时工作。
A variation of Leonid's answer which applies on an earlier stage:
After typing this in a notebook, typing
gives me
while
gives
but
gives
However it seems to only work in the notebook interface, not when using math on the command line.