我可以订购作为特征一部分加载的方法修饰符吗?

发布于 2024-09-14 02:20:47 字数 419 浏览 4 评论 0原文

这是上一个问题的后续问题。如果我有多个带有 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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

贱贱哒 2024-09-21 02:20:47

简短的回答是“否”。虽然方法修饰符是有序的,但角色自然是无序组成的。这种组合冲突是角色中的方法修饰符充满危险的原因之一,并且您很少看到它用于任何复杂的野外情况。

用户也因无序而闻名。 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.

会傲 2024-09-21 02:20:47

引用 Moose::Manual::MethodModifiers (就在为什么使用之前)他们?)。

当有多个修饰符时
相同类型,之前和周围
修饰符从最后添加到运行
第一个和之后的修饰符运行
从第一个添加到最后一个:

 2之前
     1之前
        2左右
           1左右
              小学
           1左右
        2左右
     1之后
 2之后

这个想法是,您正在修改当时存在的方法

To quote Moose::Manual::MethodModifiers (right before WHY USE THEM?).

When there are multiple modifiers of
the same type, the before and around
modifiers run from the last added to
the first, and after modifiers run
from first added to last:

 before 2
     before 1
         around 2
             around 1
                 primary
             around 1
         around 2
     after 1
 after 2

The idea is that you are modifying the method as it exists right at that moment.

晚风撩人 2024-09-21 02:20:47

有什么办法可以防止用户(程序员)以错误的顺序加载它们。

这是一个非常主观的答案,取决于您尚未描述的系统架构。但一种方法是您可以将“您自己”的角色应用到另一个角色中,然后该角色仅应用于该类:

package OmniRole;
use Moose::Role;

with 'RoleA',' RoleB', 'RoleC';  # these roles contain the 'around' method modifier


package User::Class;
use Moose;

with 'OmniRole';    # wraps method 'foo' several times to do magic.

Is there a way I can prevent the user (programmer) from loading them in the wrong order.

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:

package OmniRole;
use Moose::Role;

with 'RoleA',' RoleB', 'RoleC';  # these roles contain the 'around' method modifier


package User::Class;
use Moose;

with 'OmniRole';    # wraps method 'foo' several times to do magic.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文