rfc:retry-keyword

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:retry-keyword [2017/06/19 13:32] – Fix type-o's and stupid tabs sammykrfc:retry-keyword [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 1: Line 1:
-====== PHP RFC: Retry keyword in catch blocks ======+====== PHP RFC: Retry functionality ======
   * Version: 1.0   * Version: 1.0
   * Date: 2016-06-19   * Date: 2016-06-19
Line 135: Line 135:
 </code> </code>
  
-This is arguably the cleanest option for retrying but is blanketed with the negative stigma of using a ''goto''.+While this is arguably the cleanest option, it still requires the developer to define and manage label which over several refactors might gradually move further away from the top of the ''try'' lineThis implementation also makes it easy to accidentally execute any code after the label & before the ''try'' for each retry which is not entirely obvious at first glance. 
 + 
 +<code php> 
 +$maxTries = 5; 
 + 
 +retryTheThing: 
 + 
 +someCodeIDoNotWantToRetry(); 
 + 
 +try { 
 +    somethingSketchy(); 
 +} catch (RecoverableException $e) { 
 +    if (--$maxTries > 0) { 
 +        goto retryTheThing; 
 +    } 
 +    die('Tried a bunch but failed.'); 
 +
 +</code>
  
 ==== Use Retry ==== ==== Use Retry ====
Line 156: Line 173:
 </code> </code>
  
-Alternatively using the block-level implementation of ''retry'' keeps the developer from having to write her own boilerplate to both 1) track the number of attempts and 2) execute arbitrary code before retying.+Alternatively using the block-level implementation of ''retry'' keeps the developer from having to write her own boilerplate to both 1) track the number of attempts and 2) execute arbitrary code before retrying.
  
 <code php> <code php>
Line 232: Line 249:
 === Breaking out of retry === === Breaking out of retry ===
  
-It is sometimes necessary to have some logic that would abort any more retry attempts, like in the case of retrying forever. That can be done using the ``break`` keyword.+It is sometimes necessary to have some logic that would abort any more retry attempts, like in the case of retrying forever. That can be done using the ''break'' keyword.
  
 <code php> <code php>
Line 361: Line 378:
 ===== Proposed PHP Version ===== ===== Proposed PHP Version =====
  
-Next PHP 7.2.+Next PHP 7.3.
  
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
  
 Requires a 2/3 majority. Requires a 2/3 majority.
 +
 +==== Add block-level retry ====
 +
 +<code php>
 +try {
 +    somethingSketchy();
 +} retry 3 (RecoverableException $e, $attempt) {
 +    sleep(1);
 +} catch (RecoverableException $e) {
 +    echo $e->getMessage();
 +}
 +</code>
  
 <doodle title="Add block-level retry?" auth="sammyk" voteType="single" closed="true"> <doodle title="Add block-level retry?" auth="sammyk" voteType="single" closed="true">
Line 371: Line 400:
    * No    * No
 </doodle> </doodle>
 +
 +==== Add retry keyword ====
 +
 +<code php>
 +try {
 +    somethingSketchy();
 +} catch (RecoverableException $e)
 +    retry;
 +}
 +</code>
  
 <doodle title="Add retry keyword?" auth="sammyk" voteType="single" closed="true"> <doodle title="Add retry keyword?" auth="sammyk" voteType="single" closed="true">
rfc/retry-keyword.1497879158.txt.gz · Last modified: 2017/09/22 13:28 (external edit)