poke-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [RFC] Operator ?! for conditional on exceptions


From: Jose E. Marchesi
Subject: Re: [RFC] Operator ?! for conditional on exceptions
Date: Sun, 26 Dec 2021 12:44:51 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

> Hi, Jose.
>
> On Fri, Dec 24, 2021 at 06:44:06PM +0100, Jose E. Marchesi wrote:
>> 
>> Hi people!
>> 
>> I have written an implementation of an "excond" operator ?!, that can be
>> found in the branch jemarch/excond in poke.git.
>> 
>> This operator has two forms:
>> 
>>   EXPRESSION ?! EXCEPTION
>> 
>> and
>> 
>>   COMP_STMT ?! EXCEPTION
>> 
>> i.e. as the first operator it can get an arbitrary expression or a
>> compound statement.
>> 
>> In both cases, the ?! operator always evaluates to a boolean value.  If
>> the execution of the expression or statement raises EXCEPTION, the
>> result of the operation is 0 (false).  Otherwise it is 1 (true).  The
>> value to which EXPRESSION evaluates is discarded.
>> 
>> Simple example:
>> 
>>  if (1/zero ?! E_div_by_zero)
>>    print "division by zero\n";
>> 
>> A motivation for this feature is, given an union, an easy way to
>> determine whether a given alternative is the chosen one without having
>> to resort to a clumsy try-catch:
>> 
>>   union
>>   {
>>     int alt1 : ... ;
>>     int alt2;
>>   
>>     method _print =
>>     {
>>       if (alt1 ?! E_elem)
>>         pretty_print_alt1;
>>       else
>>         pretty_print_alt2;
>>     }
>>   }
>> 
>> An example with a statement:
>> 
>>   if ({ do_whatever_io_operation; } ?! E_eof)
>>     printf "EOF happened\n";
>> 
>> Using the operator in a statement-expression is also useful.  Consider
>> this idiom we use in pkl-rt-1.pk and other places:
>> 
>>   /* Add the directories in POKE_LOAD_PATH, if defined.  */
>>   try load_path = getenv ("POKE_LOAD_PATH") + ":" + load_path;
>>   catch if E_inval { }
>> 
>> This can now be rewritten as:
>> 
>>   /* Add the directories in POKE_LOAD_PATH, if defined.  */
>>   { load_path = getenv ("POKE_LOAD_PATH") + ":" + load_path } ?! E_inval
>> 
>> What do you people think?  Should we put this support in master?  Is the
>> syntax ok?
>> 
>> 
>
>
> I like it.
> The COMP_STMT version is a little bit unusal, but it's useful. Because
> otherwise one should write something like this:
>
> ```poke
> if (lambda int: { do_whatever_io_operation; return 0; } ?! E_eof)
>   print "EOF happend\n";
> ```
> Interestingly enough, Poke accepts this, too (note the `void`):
>
> ```poke
> if (lambda void: { do_whatever_io_operation; } ?! E_eof)
>   print "EOF happend\n";
> ```

Yes indeed :)

Thanks for the feedback.  My main worry was for the proposed syntax to
be sane, and to be sure it is not jeopardizing some future extension.
This is the first expression that starts with a {.

Allright, since nobody raised any concern I will install this today in
master once I write some documentation for the manual.

> What about supporting more exception codes?
>
> ```poke
> var not_ok = do_something ?! [E_inval, E_eof];
> ```

Hmm, I would wait a bit until we use the new construction for a while
and see if there is a need for multiple exceptions.

Thanks for the feedback!



reply via email to

[Prev in Thread] Current Thread [Next in Thread]