Implement first-class callable syntax#114
Merged
Merged
Conversation
thekid
commented
Jul 2, 2021
| protected function emitCallable($result, $callable) { | ||
| $t= $result->temp(); | ||
| $result->out->write('fn(...'.$t.') => '); | ||
| $this->emitOne($result, $callable->expression); |
Member
Author
There was a problem hiding this comment.
Once the RFC merged to PHP 8.1, we should move this to a trait for PHP 7.4 and 8.0 and emit native code on PHP 8.1
Member
Author
There was a problem hiding this comment.
Now the only thing we need to do is to remove the use CallablesAsClosures from the PHP81 emitter.
Member
Author
|
This implementation does not check for and subsequently disallow nullsafe calls as described in the PHP RFC, leaving it up to the runtime engine. This could be added easily (see below), however, it would be a first in the emitter, which doesn't throw errors up until now! diff --git a/src/main/php/lang/ast/emit/PHP.class.php b/src/main/php/lang/ast/emit/PHP.class.php
index 3a10a60..ba460e9 100755
--- a/src/main/php/lang/ast/emit/PHP.class.php
+++ b/src/main/php/lang/ast/emit/PHP.class.php
@@ -1,5 +1,6 @@
<?php namespace lang\ast\emit;
+use lang\IllegalStateException;
use lang\ast\Code;
use lang\ast\nodes\{InstanceExpression, ScopeExpression, BinaryExpression, Variable, Literal, ArrayLiteral, Block};
use lang\ast\types\{IsUnion, IsFunction, IsArray, IsMap};
@@ -989,6 +990,10 @@ abstract class PHP extends Emitter {
}
protected function emitCallable($result, $callable) {
+ if ($callable->expression->kind === 'nullsafeinstance') {
+ throw new IllegalStateException('Cannot use ?-> with callables');
+ }
+
$t= $result->temp();
$result->out->write('fn(...'.$t.') => ');
$this->emitOne($result, $callable->expression); |
thekid
commented
Jul 4, 2021
Member
Author
|
This can safely be merged, the vote is at 41:0 in favor of including this and thus very unlikely to fail. |
Calling static trait method T::__init is deprecated, it should only be called on a class using the trait
Member
Author
Member
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Examples
See also