很抱歉Java术语,但我如何重载Moose构造函数?
假设我代表一个细分。我可以采取起点和点,或起点和长度,或终点和长度。
我如何允许这种替代施工方法?
答案 0 :(得分:11)
您无需覆盖new
。 You can supply your own BUILD
:
#!/usr/bin/perl
package My::Segment;
use Moose;
use namespace::autoclean;
use Carp qw( confess );
has 'start' => (is => 'ro', isa => 'Num',
predicate => 'has_start', writer => '_set_start',
);
has 'end' => (is => 'ro', isa => 'Num',
predicate => 'has_end', writer => '_set_end',
);
has 'length' => (is => 'ro', isa => 'Num',
predicate => 'has_length', writer => '_set_length',
);
sub BUILD {
my $self = shift;
$self->has_start and $self->has_end and $self->length and do {
return if $self->length == $self->end - $self->start;
confess "Inconsistent start, end and length";
};
$self->has_start and $self->has_end and do {
$self->_set_length($self->end - $self->start);
return;
};
$self->has_start and $self->has_length and do {
$self->_set_end($self->start + $self->length);
return;
};
$self->has_end and $self->has_length and do {
$self->_set_start($self->end - $self->length);
return;
};
confess "At least two of start, end or length must be supplied";
}
__PACKAGE__->meta->make_immutable;
package main;
use YAML;
my $x = My::Segment->new(start => 0, length => 3);
my $y = My::Segment->new(start => 1, end => 4);
my $z = My::Segment->new(end => 5, length => 3);
print Dump($_) for $x, $y, $z;
my $w = My::Segment->new(start => 0, end => 0, length => 1);
答案 1 :(得分:3)
Sinan的BUILD
答案可能是最直接的解决方案。使用BUILDARGS
作为dave提到也是一个合理的解决方案。
我觉得值得一提的是也可以使用Type Coercions。给出一个类:
class LineSegment {
has [qw(startX startY endX endY)] => (
isa => 'Num',
is => 'ro',
required => 1
);
}
您可以使用一组强制:
class_type 'LineSegment';
subtype StartLength
=> as Hashref
=> where { exists $_->{startX} && $_->{startY} && $_->{length} };
subtype EndLength
=> as Hashref
=> where { exists $_->{endX} && $_->{endY} && $_->{length} };
coerce LineSegment
=> from StartLength
=> via { my ($endX, $endY) = calc_end($_);
LineSegment->new(
startX => $_->{startX},
startY => $_->{startY},
endX => $endX,
endY => $endY,
)};
coerce LineSegment
=> from EndLength
=> via { my ($startX, $startY) = calc_start($_);
LineSegment->new(
startX => $startX,
startY => $startY,
endX => $_->{endX},
endY => $_->{endY},
)};
然后在你的代码中:
use Moose::Util::TypeConstraints;
find_type_constraint('LineSegment')->coerce({
startX => $x,
startY => $y,
length => $length
});
虽然这个例子可能有些过分,但有几次强制是一种优雅的解决方案。例如,如果您有一个预先存在的LineSegment
课程,但您希望不添加length
属性(尽管BUILDARGS
也适用于此类)