Luke Palmer wrote:
Aaron Sherman writes:
Ok, so in the case of:
        my int $i = ...;
we should apply C<convert:as(..., ::int)>  and fail at run-time,
correct? There's nothing wrong with TRYING to do the conversion, just as
there should not be anything wrong with:
        my int $i = "4";
which has some pretty simple semantics in Perl.

Right. Though, constant folding at CHECK time might be able to tell when you're *going* to do an invalid conversion and complain about it right then.

In the case of ..., give it type error semantics.  That is, any
expression involving ... gets type "...".  Except instead of reporting
at the end of the statement, just suppress the errors and move on.

Huh? Um, no, your ideas as to what happens don't give the desired semantics for ..., and don't make other desired semantics fall out naturally.


The basic semantic for ... is that use of it gives an error at runtime, when the code has been hit. Unless a pragmata changes things, it should not be possible to trigger an error from use of ... without the code being actually run.

Thus, I propose the following: A convert routine should be able to tell if it's being run at runtime or compile time, and do a fail with reason matching :i/not yet/ or :i/too early/, to defer the conversion until runtime.

This gives the desired semantics for ... -- an error at runtime, not compile time. It also allows for desired semantics elsewhere. Say I have two classes, Net::IP::Addr and Net::HostName. It should be possible to convert a Net::HostName to a Net::IP::Addr, but that conversion should not happen until runtime (because I may be keeping around the bytecode for a long time, and the hostname->IP mapping may be different by then).

(Note: Aaron Sherman's syntax above doesn't match A12#Overloading. Was the syntax changed, or is he wrong?)

multi sub *coerce:as (Net::HostName $name, Net::IP::Addr ::to) {
fail 'Too early to convert hostname to IP address' if (we_are_in_compile_time);
$name.lookup;
}


(Yes, I know those parens around the condition of the if are optional -- even in perl5. I like them.)

What I don't know is how to write C<we_are_in_compile_time>. A property of the object that C<caller> gives?

I don't really like using fail with regex matching here, but we need to be able to return a real undef (we don't mind converting early, but the correct conversion is to undef -- C<0 as bool>), should be able to really fail (it's not a valid conversion, and waiting won't help anything -- C<sqrt(-1) as Real>).

BTW, since that example above isn't the hottest, imagine defining a conversion from a hostname to a DNS lookup, that saves the expiry time, and defining a second coercion from that to an IP address, that reruns the lookup if the TTL has expired. The first coercion should take place at compile time, the second not until runtime.

-=- James Mastros

Reply via email to