rfc:noreturn_type

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
Next revisionBoth sides next revision
rfc:noreturn_type [2021/03/10 17:59] – Add Ondrej's email mattbrownrfc:noreturn_type [2021/03/30 14:55] – Add __toString example mattbrown
Line 22: Line 22:
  
 <code php> <code php>
-function redirect(string $uri) : noreturn {+function redirect(string $uri): noreturn {
     header('Location: ' . $uri);     header('Location: ' . $uri);
     exit();     exit();
 } }
  
-function redirectToLoginPage() : noreturn {+function redirectToLoginPage(): noreturn {
     redirect('/login');     redirect('/login');
 } }
Line 47: Line 47:
  
 <code php> <code php>
-function redirect(string $uri) : noreturn {+function redirect(string $uri): noreturn {
     if ($uri === '') {     if ($uri === '') {
         return; // Fatal error: A noreturn function must not return         return; // Fatal error: A noreturn function must not return
Line 59: Line 59:
  
 <code php> <code php>
-function redirect(string $uri) : noreturn {+function redirect(string $uri): noreturn {
     if ($uri !== '') {     if ($uri !== '') {
         header('Location: ' . $uri);         header('Location: ' . $uri);
Line 72: Line 72:
  
 <code php> <code php>
-function generateList(string $uri) : noreturn {+function generateList(string $uri): noreturn {
     yield 1;     yield 1;
     exit();     exit();
Line 100: Line 100:
 abstract class Person abstract class Person
 { {
-    abstract public function hasAgreedToTerms() : bool;+    abstract public function hasAgreedToTerms(): bool;
 } }
  
 class Kid extends Person class Kid extends Person
 { {
-    public function hasAgreedToTerms() : noreturn+    public function hasAgreedToTerms(): noreturn
     {     {
         throw new \Exception('Kids cannot legally agree to terms');         throw new \Exception('Kids cannot legally agree to terms');
Line 117: Line 117:
 abstract class Redirector abstract class Redirector
 { {
-    abstract public function execute() : noreturn;+    abstract public function execute(): noreturn;
 } }
  
 class BadRedirector extends Redirector class BadRedirector extends Redirector
 { {
-    public function execute() : void {} // Fatal error+    public function execute(): void {} // Fatal error 
 +
 +</code> 
 + 
 +Returning by reference with a ''noreturn'' type is allowed as well. 
 + 
 +<code php> 
 +class A { 
 +    public function &test(): int { ... } 
 +
 +class B extends A { 
 +    public function &test(): noreturn { throw new Exception; } 
 +
 +</code> 
 + 
 +Returning ''noreturn'' is also allowed in ''__toString'' methods: 
 + 
 +<code php> 
 +class A implements Stringable { 
 +    public function __toString(): string { 
 +        return "hello"; 
 +    } 
 +
 + 
 +class B extends A { 
 +    public function __toString(): noreturn { 
 +        throw new \Exception('not supported'); 
 +    }
 } }
 </code> </code>
Line 157: Line 184:
  
 <code php> <code php>
-function sayHello(string $name) : void {+function sayHello(string $name): void {
     echo "Hello $name";     echo "Hello $name";
 } }
Line 168: Line 195:
  
 <code php> <code php>
-function redirect(string $uri) : noreturn {+function redirect(string $uri): noreturn {
     header('Location: ' . $uri);     header('Location: ' . $uri);
     exit();     exit();
Line 185: Line 212:
 <code php> <code php>
 #[\NoReturn] #[\NoReturn]
-function redirectToLoginPage() : void {...}+function redirectToLoginPage(): void {...}
 </code> </code>
  
Line 191: Line 218:
  
 <code php> <code php>
-function redirectToLoginPage() : noreturn {...}+function redirectToLoginPage(): noreturn {...}
 </code> </code>
  
Line 224: Line 251:
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
  
-Yes/no vote for adding ''noreturn''+Yes/no vote for adding ''noreturn'' (2/3 majority) 
 + 
 +Vote for ''noreturn'' vs ''never'' (simple majority)
rfc/noreturn_type.txt · Last modified: 2021/04/19 09:31 by nikic