On Monday 04 September 2006 16:21, Audrey Tang wrote:
> 2006/9/4, Ph. Marek <[EMAIL PROTECTED]>:
> > Excuse me for getting into this thread with only minor knowledge about
> > perl6, but will there be MMD based on the *value* of parameters? Like
> > Haskell has.
>
> Why, yes, see the various Unpacking sections in S06, as well as "where"
> type constraints.  We're st^H^Hadapting as much as we can. :-)
Hello Audrey!


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.


In Haskell you can eg. write:

        SomeThing :: Int -> Int -> Int
        SomeThing a b
          | a = 4       : b+2
          | b = 3       : a+1
          | otherwise   : a*b
or
        AnotherThing :: Int -> Int -> Int
        AnotherThing 4 b = b+2
        AnotherThing b 3 = a+1
        AnotherThing a b = a*b


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;
        }

Which is a bit wrong IMO, because the condition should be first.
But
        sub SomeThing
        {
          my($a, $b)[EMAIL PROTECTED];

          if ($a == 4) { return b+2 }
          if ($b == 3) { return a+1 }
          return a*b;
        }
is a bit of a hazzle with the {} and repeated if()s.


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 }

but without specifying the signature multiple times (or maybe we should, since 
it's MMD). Now

        sub SomeThing(Num $a, Num $b) 
        {
          if $a==4 { return $b+2;    }
          if $b==3 { return $a+1;    }
                     return $a * $b;
        }

would almost do what I want, but I don't know if the compiler would optimize 
that in the way it could for direct MMD depending on types.


Regards,

Phil

Reply via email to