Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle short closures with blocks vs. single-expression form #38

Closed
wants to merge 2 commits into from

Conversation

thekid
Copy link
Member

@thekid thekid commented Jul 4, 2022

This PR implements an extension of the Arrow Function syntax supporting multiple statements proposed by https://wiki.php.net/rfc/auto-capture-closure.

Example

$key= new Secret('...');

// Single expression form
$encrypt= fn($value, $nonce) => sodium_crypto_secretbox($value, $nonce, $key->reveal());

// Short closure with block
$decrypt= fn($cipher, $nonce) {
  if (false === ($r= sodium_crypto_secretbox_open($cipher, $nonce, $key->reveal()))) {
    throw new FormatException('Decryption failed');
  }
  return $r;
};

Diff for XP Compiler

No additional changes are required except for a couple of integration tests.

diff --git a/src/test/php/lang/ast/unittest/emit/LambdasTest.class.php b/src/test/php/lang/ast/unittest/emit/LambdasTest.class.php
index 92e1999..94b3718 100755
--- a/src/test/php/lang/ast/unittest/emit/LambdasTest.class.php
+++ b/src/test/php/lang/ast/unittest/emit/LambdasTest.class.php
@@ -189,8 +189,9 @@ class LambdasTest extends EmittingTest {
     Assert::equals('IIFE', $r);
   }
 
+  /** @deprecated */
   #[Test]
-  public function with_block() {
+  public function with_arrow_and_block() {
     $r= $this->run('class <T> {
       public function run() {
         return fn() => {
@@ -199,6 +200,21 @@ class LambdasTest extends EmittingTest {
         };
       }
     }');
+    \xp::gc(); // Clear deprecation notice
+
+    Assert::equals(2, $r());
+  }
+
+  #[Test]
+  public function with_block() {
+    $r= $this->run('class <T> {
+      public function run() {
+        return fn() {
+          $a= 1;
+          return $a + 1;
+        };
+      }
+    }');
 
     Assert::equals(2, $r());
   }
@@ -208,7 +224,7 @@ class LambdasTest extends EmittingTest {
     $r= $this->run('class <T> {
       public function run() {
         $a= 1;
-        return fn() => {
+        return fn() {
           return $a + 1;
         };
       }

See also

@thekid
Copy link
Member Author

thekid commented Jul 17, 2022

The RFC has been declined with a vote of 27 in favor and 16 against.

https://externals.io/message/118154#118270

@thekid thekid closed this Jul 17, 2022
@thekid thekid deleted the feature/auto-capture-closure branch July 17, 2022 09:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant