あれれ?? と思ったんですが、やってみると確かにエラーになりました。
残念なことに,いまのMooseではこのようなアトリビュートのアクセサに対するrequiresは(メタオブジェクト上ではメソッドとして勘定されているにもかかわらず)エラーになってしまいます。
ただ、一応requiresをhasで満たすことは考えられているようで、テスト 004_role_composition_errors.t 内にはこの項目があります。
{
package Quux::Role;
use Moose::Role;
requires qw( meth1 meth2 meth3 meth4 );
}
... 中略 ...
{
package Quux::Class4;
use Moose;
sub meth1 { }
has 'meth2' => ( is => 'ro' );
::throws_ok { with('Quux::Role') }
qr/'Quux::Role' requires the methods 'meth3' and 'meth4' to be implemented by 'Quux::Class4'/,
'exception mentions all the require methods that are accessors at once, as well as missing methods, but not the one that exists';
}
あー、withをhasの後に書けばいいってことですか。まあ、そりゃそうですけど、
package MooseObj::Role::Print; use Moose::Role; requires 'name'; sub print { my $self = shift; print $self->name; } package MooseObj; use Moose; has 'name' => (is => 'rw', isa => 'Str', default => 'foo'); with 'MooseObj::Role::Print';
と書くのは直感的じゃない気がします。
package MooseObj::Role::Print; use Moose::Role; requires 'name'; sub print { my $self = shift; print $self->name; } package MooseObj; use Moose; with 'MooseObj::Role::Print'; BEGIN { has 'name' => (is => 'rw', isa => 'Str', default => 'foo'); }
なんてのも考えましたけど、これもかなりアレっぽいです。ウーン、確かに悩ましい・・・。