develooper Front page | perl.perl5.porters | Postings from September 2012

Re: given/when/~~ "final" thoughts (ha ha ha)

Thread Previous | Thread Next
September 25, 2012 07:41
Re: given/when/~~ "final" thoughts (ha ha ha)
Message ID:
Ricardo Signes writes:

> * Father Chrysostomos <> [2012-09-16T16:01:31]
> > Have you noticed the sprout/smartpatch branch? I found Smylers’
> > argument (<>) convincing so I
> > implemented it like this:
> > 
> >  $x ~~ undef
> >  $x ~~ $overloaded
> >  $x ~~ sub{}
> >  $x ~~ qr//
> >  $x ~~ $anthing_else # fall back to eq
> I like falling back to eq and had proposed it at one point, but there
> was so much support of smartly picking == or eq

There were requests for == to be supported as well as eq, but when I

  In what situations are numeric comparisons with C<when> required?


nobody responded with a realistic scenario in which it's a useful thing
to do.

Of course, Warnock's Dilemma applies: it could be that there are dozens
of useful scenarios, but by the time I asked everybody was bored with
smartmatch and couldn't face reading through the thoughts of a relative

Damian previously gave an example where == matching would work but eq
wouldn't, because $_ has been populated with readline and has a "\n":

  while (readline) {
      when (undef)    { say 'done'; last;  }
      when (0)        { say 'must be +ve'; }
      when (any 1..9) { say 'digit';       }
      default         { die 'horribly';    }


However that can be remedied with a simple chomp at the start of the
loop. And if a non-digit option were to be added, say:

      when (any qw<h ?>) { display_help(); }

that would have to use eq matching, so requires the chomp. It could be
surprising to have to add in a chomp to make that work, when the cases
above have already been working fine without it.

It is of course nice and convenient if code like the above works even if
you don't think to put a chomp in there. But it isn't clear to me why
that convenience should apply only when the input is purely numeric: it'd be
just as convenient to have the trailing newlines ignored when matching
any other strings.

From a teaching point of view it seems simpler, and less special-casey,
to say when does string matching -- and for Damian's original example to
die horribly from the start (at which point the teaching point is simply
"readline returns complete lines, including the trailing \n"; use chomp
to strip them), rather than the first time a non-numeric option is added
to the menu (when the teaching point is, um ... rather more

> > And when() just does smartmatch.
> So, in your branch, when(5) is stringy, and if you want numeric, you say
> when { 5 == $_ } ?

Yes. Or, depending on what the numbers actual mean, perhaps actually
something like this would be more useful in practice:

  when (Number::Tolerant->new(5 plus_or_minus 0.01))

(Subject to Number::Tolerant doing ~~ overloading, which it probably
should do anyway, and for which I've just sent a pull request.)

> I really do not relish the idea of all the required work being done
> only to realize that we've gone from unbearable to just barely
> bearable.

Thank you for all the effort you've put into this so far. For what it's
worth, I think a working given and when would be useful enough to be
worth doing. Reading your plans for it were sufficiently interesting
that they've already inspired me to subscribe to P5P and write the above
Number::Tolerant patch.

New series of TV puzzle show 'Only Connect' (some questions by me)
Mondays at 20:30 on BBC4, or iPlayer:

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About