我可以订购作为特征一部分加载的方法修饰符吗?
这是上一个问题的后续问题。如果我有多个带有 around
修饰符的插件/特征,是否可以确保一定的执行顺序(因为我无法确定哪个会实际加载)?或者我真的只能在我编写的代码和文档中控制它吗?
示例:我有 3 个角色,每个角色都有一个 around
,并且每个角色都可以作为 Trait 选择性加载,但如果加载的角色超过 1 个,则必须按特定顺序加载它们一起工作。例如,按该顺序加载 Trait ABC 工作正常,但像 Trait BAC 那样加载会导致不稳定的行为。
有没有办法防止用户(程序员)以错误的顺序加载它们。
This is a follow up to a previous question. if I have multiple plugins/traits with around
modifiers, is it possible to ensure a certain execution order (seeing as how I can't be sure which will actually get loaded)? or can I really only control that in code I write and with documentation?
Example: I have 3 Roles each with an around
and each can be loaded optionally as a Trait but if more than 1 is loaded they have to be loaded in a certain order to work right together. e.g. loading Trait A B C in that order with work fine but loading it like Trait B A C will result in wonky behavior.
Is there a way I can prevent the user (programmer) from loading them in the wrong order.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
简短的回答是“否”。虽然方法修饰符是有序的,但角色自然是无序组成的。这种组合冲突是角色中的方法修饰符充满危险的原因之一,并且您很少看到它用于任何复杂的野外情况。
用户也因无序而闻名。 Moose 提供了很少的工具来强制角色的有序组成(按设计)。用户空间中肯定没有任何东西会限制它们。
也就是说,您可以在不同的回复中使用 Ether 的建议,创建一个由按正确顺序组成的特征组成的元特征。或者,您可以跳出深度并重新编写 Moose 的角色组合片段,以保证组合的有序性。老实说,我不能推荐这两种方法中的任何一种,这两种方法似乎都在我看来是糟糕设计的明显标志。
在不了解您的具体问题的更多信息的情况下,我无法提出更好的解决方案。我通常会寻找类似注册回调并以某种方式触发它的东西,而不是依赖方法修饰符。
Short answer is "No". While method modifiers are ordered, Roles are naturally composed un-ordered. This conflict of composition is one of the reasons method modifiers in Roles are fraught with peril and you rarely see it used for anything complex in the wild.
Users, also, are particularly known for being unordered. Moose provides few tools to enforce the ordered composition of Roles (by design). There is certainly nothing in user space that will constrain them.
That said, you can use Ether's suggestion in a different reply, creating a meta-Trait that consists of the traits composed in the proper order. Alternatively you can jump off the deep end and re-write the Role Composition pieces of Moose to guarantee ordered composition. I honestly can't recommend either of these approaches, both seem to be plastering over what to me would be a clear indication of poor design.
Without knowing more about your particular problem I can't suggest a better solution though. I would generically look for something like registering a callback and triggering that somehow rather than relying upon method modifiers.
引用 Moose::Manual::MethodModifiers (就在为什么使用之前)他们?)。
这个想法是,您正在修改当时存在的方法。
To quote Moose::Manual::MethodModifiers (right before WHY USE THEM?).
The idea is that you are modifying the method as it exists right at that moment.
这是一个非常主观的答案,取决于您尚未描述的系统架构。但一种方法是您可以将“您自己”的角色应用到另一个角色中,然后该角色仅应用于该类:
This is a very subjective answer and depends on the architecture of your system, which you have not described. But one way is that you could apply the roles "yourself" in another role, which is then solely applied to the class: