"Wizard" <[EMAIL PROTECTED]> writes:
> This came up on perl6-internals, and Dan liked the "try" suggestion and
> suggested That I post it here for comments. I'm not subscribed to
> p6-language, so you'll need to include me in any replies where you want a
> response from me.
> =============================================================
> Dave Mitchell <[EMAIL PROTECTED]> wrote:
>> my Complex $c = 3+4i;
>> my $plain = 1.1;
>> $plain = $c;
>
> This might be even more "Complex" than that - what if Complex can be
> reduced? Should it? for instance:
>
> my Complex $c = 3+4i;
> my Complex $d = 4i;
> my $plain = $c / $d;
>
> Does $plain (which is actually '3' after reducing) get promoted to Complex,
> or does the result from the division get demoted?

In general I'd say that $plain shouldn't change class (I'm not sure
it's being demoted; after all, a real number has more constraints on
it than a complex number), for the same reason that

Rectangle.new(sides => [1, 1]).isa('Square');

should return false.

> ....
> Perhaps there could be a sort of 'try' for conversion that returns the best
> possible result? for instance:
>
> my Complex $c = 3+4i;
> my Complex $d = <unknown qty>;
> my $plain = try_demote( $c / $d );
>
> $plain now ISA Complex if it couldn't demote the result of the math, or it
> ISA scalar (int or float) if it could. Now if you need to know, then just
> check:
>
> $plain = try_demote( $c / $d );
> # the '(or)'s here are alternate forms, not comparison
> if( $plain.type == "Complex" (or) $plain.Complex ){
>    print "It promoted!\n";
> }
> elsif( $plain.type == "Scalar" (or) $plain.Scalar ) {
>    print "Result was reduced!\n";
> }

Demote is a really bad name I think.

$plain = most_appropriate_type($c / $d);

might be better, but it's still ugly.

-- 
Piers

   "It is a truth universally acknowledged that a language in
    possession of a rich syntax must be in need of a rewrite."
         -- Jane Austen?

Reply via email to