Mathematica:OptionValue 是如何实现的?
内置 OptionValue
的实现包含一些魔法,这样
OptionValue[name]
相当于OptionValue[f, name]
,其中f
是 左侧的头部 变换规则,其中 出现OptionValue[name]
。
有谁知道如何实现类似于 Options
的功能,即实现一个 autoOptions[]
,它将解析为左侧符号定义的选项autoOptions[]
出现的转换规则? 的方法
Options[foo]={bar->1};
foo[OptionsPattern[]]:=autoOptions[]
foo[]
为了清楚起见,我正在寻找一种使输出 {bar->1}
,最终目标是执行 这个问题,除了定义的 RHS 之外,无需更改任何内容。
The implementation of the built-in OptionValue
contains some piece of magic so that
OptionValue[name]
is equivalent toOptionValue[f, name]
, wheref
is the
head of the left-hand side of the
transformation rule in whichOptionValue[name]
appears.
Does anybody have an idea for how to achieve something similar for Options
, i.e. implement an autoOptions[]
that would resolve to the options defined for the symbol on the left hand side of the transformation rule in which autoOptions[]
appears?
For clarity, what I am looking for is a way to make
Options[foo]={bar->1};
foo[OptionsPattern[]]:=autoOptions[]
foo[]
output {bar->1}
The eventual goal is to do something like requested in this question without having to change anything but the RHS of a definition.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个简单、非常示意性的版本:
您的用法:
请注意,当还传递显式选项时,这将不起作用 - 考虑它们需要更多工作,而且这通常不是一个好的做法,因为我重载了
SetDelayed< /code> - 但你要求它并且你得到它。
Here is a simple, very schematic version:
Your usage:
Note that this won't work when explicit options are also passed - accounting for them is some more work, and this is not generally a good practice since I overloaded
SetDelayed
- but you asked for it and you get it.