On 2009-Feb-23, Jonathan Worthington posted to 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.

2) Override it by defining an explicit infix:<min=> operator that does max=, and vice versa. People like me will never notice that something fishy is going on, but it penalizes the folks who are actually paying attention to what "min=" ought to mean!

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


Reply via email to