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?
....
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";
}

Ramblings of a madman,
Grant M.


Reply via email to