develooper Front page | perl.perl5.porters | Postings from April 2009

Re: feature 'switch' as an expression?

Thread Previous | Thread Next
From:
Matt S Trout
Date:
April 30, 2009 15:53
Subject:
Re: feature 'switch' as an expression?
Message ID:
20090430225249.GB17537@agaton.scsys.co.uk
On Wed, Apr 29, 2009 at 10:01:02PM +0100, Hakim Cassimally wrote:
> 2009/4/29 Mark Mielke <mark@mark.mielke.cc>:
> > Hakim Cassimally wrote:
> >>
> >> I wanted to be able to write:
> >>
> >>    my $x = given ($blah) {
> >>        ....
> >>        default { 'foo' }
> >>       }
> >>
> > Seems like along that path would lie:
> >
> >   my $x = if ($blah) { ... } else { 'foo' };
> >
> > Some part of me finds your expectation appealing and some part of me is
> > revolted. :-)
> 
> Perhaps I've been programming too much Haskell... I certainly don't
> see why a case statement *shouldn't* work as an expression.  (And it
> doesn't turn my stomach either ;-)
> 
> (And why resort to the chain of ?: ternaries, given that that's the
> kind of ugliness that the switch was brought in to deal with in the
> first place?)
> 
> Actually, I don't dislike the idea of an if-expression either, but to
> be honest, for a single expression a ternary is neater.

As noted upthread, a do {} block makes if work fine as an expression - I
do this quite a bit to clarify complex ternaries. given should, really,
also work in this context IMO, and I can't see that we'd lose anything
by doing so.

Perhaps you could have a go at writing a test to give people a basis to
work from?

-- 
        Matt S Trout         Catalyst and DBIx::Class consultancy with a clue
     Technical Director      and a commit bit: http://shadowcat.co.uk/catalyst/
 Shadowcat Systems Limited
  mst (@) shadowcat.co.uk        http://shadowcat.co.uk/blog/matt-s-trout/

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About