class Interval {
has Real $.lb is rw = die 'Lower bound is required';
has Real $.ub is rw = die 'Upper bound is required';
}
### Example which does not work:
# class Interval {
# has Real $.lb where * <= $.ub is rw = die 'Lower bound is required';
# has Real $.ub where * >= $.lb is rw = die 'Upper bound is required';
# after lb { say "Lower bound was changed!" if @_ }
# }
my $i = Interval.new( lb => 1, ub => 3);
say $i.perl;
$i.lb = 0; # should print message
say $i.perl;
$i.lb = 6; # should blow up!
say $i.perl;
####
class Interval {
has Real $.lb = die 'Lower bound is required';
has Real $.ub = die 'Upper bound is required';
method update(:$lb = $.lb, :$ub = $.ub) {
die "Require lb <= ub" unless $lb <= $ub;
($!lb, $!ub) = ($lb, $ub);
}
}
####
class Interval {
has Real $.lb = die 'Lower bound is required';
has Real $.ub = die 'Upper bound is required';
multi method lb() { $!lb }
multi method lb($lb) {
die "Require lb <= ub" unless $lb <= $!ub;
$!lb = $lb;
}
multi method ub() { $!ub }
multi method ub($ub) {
die "Require lb <= ub" unless $!lb <= $ub;
$!ub = $ub;
}
}