【问题标题】:Can MooseX::Method::Signatures be used with non-Moose classes?MooseX::Method::Signatures 可以与非 Moose 类一起使用吗?
【发布时间】:2012-01-18 10:25:25
【问题描述】:

我尝试了以下方法:

package Foo;
sub new {
    my ($type) = @_;
    return bless {}, $type;
}

package Bar;
use Moose;

package Baz;
use Moose;
use MooseX::Method::Signatures;

method exec1 (Foo $f, Bar $b) {
    ...;
}
method exec2 ($f where {$_->isa('Foo')}, Bar $b) {
    ...;
}

我发现 exec2 可以工作,但是对于 exec1 有一个编译时错误。如果 Foo 是 Mouse 对象(这些是我无法更改的类),则会发生同样的错误。

'Foo' could not be parsed to a type constraint - maybe you need to pre-declare the type with class_type at C:/strawberry/perl/site/lib/Parse/Method/Signatures/TypeConstraint.pm line 74

但是有没有办法可以使用 exec1 签名?

【问题讨论】:

    标签: perl moose


    【解决方案1】:

    在方法声明之前添加:

    use Moose::Util::TypeConstraints;
    BEGIN { class_type 'Foo'; }
    

    对于 MXMS 来说,错误很明显;)

    【讨论】:

      【解决方案2】:

      可能 Method::Signatures 对你有用,特别是如果你不使用 Moose 并且只需要签名。

      【讨论】:

      • 建议很中肯,但我还是想看看上面的实际情况能不能解决。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多