David (>), Jonathan on Rakudo.org (>>): >> Applied a patch from bacek++ to get "min= and "max=" working ("$foo min= >> 100" will assign 100 to $foo if it's smaller than what is in $foo). > > Nice -- except I first read that backwards, i.e. even though it follows > straight from the definition of "[op]=", it made me think of forcing a > minimum value, which would actually be max=. > > I can think of a few ways to deal with this: > > 1) Get used to it. Maybe document it somewhere to catch fewer people > off-guard, but otherwise simply expect people to check their code.
For what it's worth, I write a lot of Perl 6, and I'm already used to it. I don't think it's backwards, and hence I don't think any action need be taken to reverse its meaning or go out of one's way to find other ways to say the same thing. I simply read '$foo min= 100' as 'set $foo to the minimum of itself and 100', just as I think of '$foo += 100' as 'set $foo as the sum of itself and 100'. > 3) Avoid the potential for confusion by having another way to set min/max > values, such as: > $foo.min = 100; > min($foo) = 100; # lvalue function > $foo = get-foo() :min(100); # adverb on = > > I'm not crazy about any of those, but that did suggest what is probably a > better way to approach it anyway: setting limits at declaration rather than > repeating them every time the variable is assigned to: > > my $foo is limited(100..200); > $foo = 5; # really does $foo = 100 Sounds like a good idea for a CPAN module. You can already do something similar with the subset keyword, though: subset Percentage of Int where { 0 <= $^perc <= 100 }; sub set_percentage(Percentage $perc is rw, Int $value) { try { return $perc = $value; CATCH { return $perc = 0 if $value < 0; return $perc = 100 } } }; my Percentage $p; say set_percentage($p, 50); say set_percentage($p, 101) // Carl