develooper Front page | perl.perl6.language | Postings from July 2010

Re: Smart match isn't on Bool

Thread Previous | Thread Next
From:
David Green
Date:
July 31, 2010 19:15
Subject:
Re: Smart match isn't on Bool
Message ID:
F4B5528D-2DA8-4175-8428-A96AEEA2D416@telus.net
On 2010-07-31, at 5:55 pm, Darren Duncan wrote:
> I would prefer if given/when was nothing more than an alternate syntax for if/then that does comparisons. 

And that breaks out of its enclosing scope.

> On the other hand, Perl 6 has multiple equality comparison operators, eqv, eq, ==, ===, etc, and hence ~~ semantics instead of eqv may seem to make some sense, though I think that would cause more problems.

Yes, since if you've got a whole list of when's to compare against, it's not unlikely that they're different types of things, so ~~ would be more useful than === or eqv.  Of course, given the bool-exception spec, one workaround is "when $_ eqv $x".  But I was already thinking of a more elegant possibility.

Using "when ~~ $bar" is problematic because Perl couldn't know whether to expect a term or an operator after "when".  But what if ~~ were merely the default operator, and you could override it?

    given $foo :using( [===] ) { ... }
    given $foo :using(&binary-func) { ... }

such that any 'when's inside the block use ===, or the provided operator/function instead, of ~~.  Actually, the option should be on 'when', so that above line would mean something like:

    given $foo { my &when = &when.assuming( :using( [===] )) ... }

...er, except 'when' isn't a function, so that doesn't actually work.  Anyway, the idea is that there could be some way to set the default operation to use for 'when' in a given scope.  


Here's another thought: 'when' is different from 'if' in two ways.  One is that it does an implicit comparison on the topic; the other is that it breaks out of its enclosing block.  Maybe the comparison could be indicated another way, leaving 'when' and 'if' to differ in breaking out or not.  Suppose a colon indicated "compare against $_ using ~~, or whatever the default operation is" (we're not using the colon for anything else, are we?!?):

    when $a > $b { ... }
    if $foo.does($bar) { ... }
    
    when: /foo/ { ... }
    if: .defined { ... }


-David


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