类型约束 'XYZ' 已经创建 [英] The type constraint 'XYZ' has already been created

查看:26
本文介绍了类型约束 'XYZ' 已经创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Moose::Util::TypeConstraints 在我的应用程序中.

I want to use a Moose::Util::TypeConstraints in my application.

所以我在我的 main.pl

use Moose::Util::TypeConstraints;

subtype 'mySpecialType'
    => as 'Object'
    => where sub { $_->does('something') };

use noUse;

noUse.pm中是使用的包,它们使用类型约束

In the package noUse.pm are packages used, which use the type constraint

package noUse;

use Use1;

use Use2;

1;

和我的包 Use1Use2 正在使用类型约束

and my package Use1 and Use2 are working with the type constraint

package Use1; 

use Moose; 

has 'object1' => ( is => 'ro', isa => 'mySpecialType' ); 

1;

使用2.pm

package Use2; 

use Moose; 

has 'object2' => ( is => 'ro', isa => 'mySpecialType' ); 

1;

如果我运行 main.pl 我得到这个错误:

If I run main.pl I get this error:

类型约束 'mySpecialType' 已经在 Use1 中创建,并且不能在 main 的/usr/lib/x86_64-linux-gnu/perl5/5.22/Moose/Util/TypeConstraints.pm line 348 中再次创建Moose::Util::TypeConstraints::subtype('mySpecialType', 'HASH(0x227f398)', 'HASH(0x2261140)') 在 main.pl 第 10 行调用

The type constraint 'mySpecialType' has already been created in Use1 and cannot be created again in main at /usr/lib/x86_64-linux-gnu/perl5/5.22/Moose/Util/TypeConstraints.pm line 348 Moose::Util::TypeConstraints::subtype('mySpecialType', 'HASH(0x227f398)', 'HASH(0x2261140)') called at main.pl line 10

是什么导致了这个错误,我该如何解决?

What causes this error and how can I fix it?

推荐答案

标准的方式是在需要的地方使用库中的公共代码,而不是让它从主程序中辐射出来.

The standard way is to use the common code from the libraries where it's needed, not to let it radiate from the main program.

package MyTypes;
use Moose::Util::TypeConstraints;
subtype 'mySpecialType'
    => as 'Object'
    => where sub { $_->does('something') };

使用1.pm

package Use1; 
use Moose; 
use MyTypes;
has 'object1' => ( is => 'ro', isa => 'mySpecialType' ); 
1;

使用2.pm

package Use2; 
use Moose;
use MyTypes; 
has 'object2' => ( is => 'ro', isa => 'mySpecialType' ); 
1;

main.pl 就变成了

use noUse;

noUse.pm 保持不变.

参见 simbabque回答为什么.

这篇关于类型约束 'XYZ' 已经创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆