On Mon, Sep 04, 2006 at 10:52:35PM -0700, Trey Harris wrote:
: In a message dated Tue, 5 Sep 2006, Ph. Marek writes:
: >I now had a look at http://dev.perl.org/perl6/doc/design/syn/S06.html 
: >but didn't find what I meant. Sorry if I'm just dumb and don't 
: >understand you (or S06); I'll try to explain what I mean.
: 
: I don't think you're dumb; the Synopses just require that you intuit 
: certain things from each other, from examples in other Synopses, and so on 
: in a Perlish sort of way; what you're looking for is not spelled out 
: explicitly.  It can be found by noticing how you specify subtypes, along 
: with noticing that subtypes can be specified as parameter types.  There's 
: also an example showing explicitly what you want in S12.
: 
: >In Perl5 this looks like
: >
: >     sub SomeThing
: >     {
: >       my($a, $b)[EMAIL PROTECTED];
: >
: >       return b+2 if ($a == 4);
: >       return a+1 if ($b == 3);
: >       return a*b;
: >     }
: >[...]
: >What I am asking is whether there will be some multimethod dispatch 
: >depending
: >on the *value*, not the *type*, of parameters.
: >Perl6 could possibly do something with "given"; but matching on multiple
: >variables seems to be verbose, too.
: >I'm looking for something in the way of
: >
: >     sub SomeThing(Num $a, Num $b) where $a==4 is $b+2;
: >     sub SomeThing(Num $a, Num $b) where $b==3 is $a+1;
: >     sub SomeThing(Num $a, Num $b) { return $a * $b }
: 
: It's just
: 
:    multi sub SomeThing(Num $a where {$^a == 4}, Num $b) { $b + 2  }
:    multi sub SomeThing(Num $a, Num $b where {$^b == 3}) { $a + 1  }
:    multi sub SomeThing(Num $a, Num $b)                  { $a * $b }
: 
: >but without specifying the signature multiple times (or maybe we should, 
: >since
: >it's MMD). Now
: 
: Yes, the signatures are different--the first two multis specify subtypes 
: as their signatures, the last specifies a canonical type.

Every scalar value is a one-element subset of its type, so you can just write:

   multi sub SomeThing(Num 4, Num $b)  { $b + 2  }
   multi sub SomeThing(Num $a, Num 3)  { $a + 1  }
   multi sub SomeThing(Num $a, Num $b) { $a * $b }

or even just

   multi sub SomeThing(4, Num $b)      { $b + 2  }
   multi sub SomeThing(Num $a, 3)      { $a + 1  }
   multi sub SomeThing(Num $a, Num $b) { $a * $b }

Though 3 and 4 are arguably going to match against Int rather than Num...

Larry

Reply via email to