加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl – 麋鹿的角色和性状如何不同?

发布时间:2020-12-15 21:21:03 所属栏目:大数据 来源:网络整理
导读:我已经编写了一套在 Moose中也使用角色实现的类和接口。我无法理解的是在使用和实现麋鹿特征与角色方面的确切差异。 Moose documentation规定: It is important to understand that roles and traits are the same thing. A role can be used as a trait,an
我已经编写了一套在 Moose中也使用角色实现的类和接口。我无法理解的是在使用和实现麋鹿特征与角色方面的确切差异。

Moose documentation规定:

It is important to understand that roles and traits are the same thing. A role can be used as a trait,and a trait is a role. The only thing that distinguishes the two is that a trait is packaged in a way that lets Moose resolve a short name to a class name. In other words,with a trait,the caller can refer to it by a short name like “Big”,and Moose will resolve it to a class like MooseX::Embiggen::Meta::Attribute::Role::Big.

我的理解是,特质和角色是“一样??的”。然而,当使用使用Moose-traits’Foo’语法来实现对这个想法的基本测试似乎并没有达到我期望的程度。当然我一定是在这里遗漏的东西。

这个第一个例子失败了“无法找到对象方法”foo’“

package MyApp::Meta::Class::Trait::HasTable;
use Moose::Role;
sub foo { warn 'foo' }

package Moose::Meta::Class::Custom::Trait::HasTable;
sub register_implementation { 'MyApp::Meta::Class::Trait::HasTable' }

package MyApp::User;
use Moose -traits => 'HasTable';
__PACKAGE__->foo();  #Can't locate object method 'foo'

与此相比(这样做):

package MyApp::Meta::Class::Trait::HasTable;
use Moose::Role;
sub foo { warn 'foo' }

package Moose::Meta::Class::Custom::Trait::HasTable;
sub register_implementation { 'MyApp::Meta::Class::Trait::HasTable' }

package MyApp::User;
use Moose;
with 'MyApp::Meta::Class::Trait::HasTable';
__PACKAGE__->foo();  #foo

解决方法

这是Moose如何使用术语“特征”和“角色”的唯一区别。
Moose的文档和API通常使用术语“traits”作为“应用角色”
到Metaclasses“。在你的修改后的答案中,你的第一个例子应用了角色
MyApp ::用户的metaclass通过-traits,第二个例子应用于
类。

如果您将您的第一个例子更改为:

package MyApp::Meta::Class::Trait::HasTable;
use Moose::Role;
sub foo { warn 'foo' }

package Moose::Meta::Class::Custom::Trait::HasTable;
sub register_implementation { 'MyApp::Meta::Class::Trait::HasTable' }

package MyApp::User;
use Moose -traits => 'HasTable';
__PACKAGE__->meta->foo();

你会看到“foo at [script]。line 3”这正是它应该是什么
在做

更新:显然我在这里并不完全正确。特征是应用于实例的角色。 therara钩子将HasTable应用于MyApp :: User的元类实例。我已经更新了相关的Moose文档。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读