HaloO,

Smylers wrote:
TSa writes:

... one question remains that might actually make Duncan's proposal
pointless.

Didn't Larry actually accept Darren's proposal and add the C<before> and
C<after> operators?

He did. But I don't understand exactly how the coercive semantics
of  the numeric and string comparisons are achieved. I assume there
are protos defined with signature :(Any,Any) that coerce the args
to numeric or string and re-dispatch. Right? IIRC, these protos
kick in when dispatch ends in a draw.


Are the comparison operators available for overloading just like any
other operator

Yes.  This feature is used often enough in Perl 5 classes, and I'm
almost certain there are no plans to remove it from Perl 6.

That sounds good and places the numeric and string comparison ops
close to generic. I think that there will be a role as follows:

role Order[Str $lt  = '<',
           Str $gt  = '>',
           Str $le  = '<=',
           Str $ge  = '>=',
           Str $cmp = '<=>'
           Str $eq  = '==' ] does Compare[$eq] # provides === and eqv
{
    enum <Increase(1) Same(0) Decrease(-1)>;
    # these two need to be provided
    multi infix<before> ( ::?CLASS $one, ::?CLASS $two ) {...}
    multi infix<after>  ( ::?CLASS $one, ::?CLASS $two ) {...}
    # the other ops can be expressed in terms of before and after
    multi infix:«$lt» ( ::?CLASS $one, ::?CLASS $two )
    {
       $one  before $two
    }
    multi infix:«$gt» ( ::?CLASS $one, ::?CLASS $two )
    {
       $one  after  $two
    }
    multi infix:«$le» ( ::?CLASS $one, ::?CLASS $two )
    {
       $one !after  $two
    }
    multi infix:«$ge» ( ::?CLASS $one, ::?CLASS $two )
    {
       $one !before $two
    }
    multi infix:«$cmp»( ::?CLASS $one, ::?CLASS $two )
    {
       if    $one before $two { return Increase }
       elsif $one ===    $two { return Same }
       elsif $one after  $two { return Decrease }
       else                   { return undef } # ?
    }
}

With that you can write

   class Str does Order[|<lt gt le ge leg eq>] {...}

But that is a bit clumsy and I would like to call the role
as does Order[Str] to get the string comparison ops and as
Order[Num] for the numeric ones. Order[Str|Num] would bring
in both and all other Order[Foo] would just get the generic
before and after. How does one write such a role? I mean you
need to match a type parameter with Str and Num and prepare
the corresponding multis for composition.


Regards, TSa.
--

Reply via email to