Search code examples
perlmoose

Can MooseX::Method::Signatures be used with non-Moose classes?


I tried it with the following:

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) {
    ...;
}

I found that exec2 can work, but for exec1 there is a compile time error. The same error occurs if Foo is a Mouse object (these are classes which I'm not able to change).

'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

But is there a way it could work with the exec1 signature?


Solution

  • Add this before the method declaration:

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

    The error was pretty clear, for MXMS anyway ;)