Comments (10)
We chose Closure
for a few reasons:
- The
callable
type in PHP is scope-dependent. What is callable in one scope may not be callable when attempting to invoke it from another scope (Luckily this problem will go away in PHP 9: https://wiki.php.net/rfc/deprecate_partially_supported_callables) (EDIT: I was wrong, that RFC does not completely clean up scope issues, visibility problems can still happen withcallable
, but cannot withClosure
.) callable
cannot be used as a class property (largely due to the above), so is more cumbersome to use.- First-class callable syntax makes it easy to create closures.
[$this, 'method']
being a "callable" has always been strange IMO.$this->method(...)
is far superior, clearly indicating that a closure is being created. - Using
Closure
does allow us to widen the type tocallable
at a later time without BC breaks
Invokable objects unfortunately suffer because of this decision. First-class callable syntax does work with invokable objects. async($object(...))
is clear that the intention is to pass a closure to be called. async($object)
may be be confused for passing the object to be manipulated or a mistake if $object
were not an invokable object.
I'm willing to consider changing Closure
to callable
in the future, but for the near term I think I'd like to keep Closure
.
from amp.
Fair enough, although I don't see how any of those 2 disadvantages are a factor for util functions. First one is more for authors of callables (me), second one for classes, while amp util functions are not classes. But of course, my position is purely from trying to have slightly nicer syntax. $object->method(...)
looks fine in such trivial example, but what about multi line calls? With callables accepted, I could do
with callables accepted, I could do
async(new Responder(
$logger,
$bufferer,
$output,
$this->customHttpHeaders,
new ResourceInputStream(fopen($bufferer->filePath, 'rb')),
$newConnDefer
));
without them, I have to do either
async((new Responder(
$logger,
$bufferer,
$output,
$this->customHttpHeaders,
new ResourceInputStream(fopen($bufferer->filePath, 'rb')),
$newConnDefer
))(...));
or
$responder = new Responder(
$logger,
$bufferer,
$output,
$this->customHttpHeaders,
new ResourceInputStream(fopen($bufferer->filePath, 'rb')),
$newConnDefer
);
async($responder(...));
and I'm not a fan of either. But again, it's something we all can live with, just adding my suggestion for minor improvement.
from amp.
async(fn () => new Responder(
$logger,
$bufferer,
$output,
$this->customHttpHeaders,
new ResourceInputStream(fopen($bufferer->filePath, 'rb')),
$newConnDefer
));
from amp.
Actually, in my cases I also need to pass arguments to Responder so above wouldn't work. with callable support I would be able to do
async(
new Responder(
$logger,
$bufferer,
$output,
$this->customHttpHeaders,
new ResourceInputStream(fopen($bufferer->filePath, 'rb')),
$newConnDefer
),
$socket,
);
but with arrow functions it would be
async(
fn($arg) => (new Responder(
$logger,
$bufferer,
$output,
$this->customHttpHeaders,
new ResourceInputStream(fopen($bufferer->filePath, 'rb')),
$newConnDefer
))($arg),
$socket,
);
which doesn't bring benefit over
async(
(new Responder(
$logger,
$bufferer,
$output,
$this->customHttpHeaders,
new ResourceInputStream(fopen($bufferer->filePath, 'rb')),
$newConnDefer
))(...),
$socket,
);
from amp.
What in the hell are you talking about?
from amp.
from amp.
Yes but that's arguably worse than https://3v4l.org/ZePLZ (desired) or https://3v4l.org/GZ5C8 ((...)
syntax)
from amp.
Stop trying to code other people's libraries for them.
from amp.
Well, this doesn't look like it's going to be a productive discussion. Sorry that my fresh feedback from amp 2.x -> 3.x update experience bothered you. I was used to pass callable to asyncCoroutine, but this is no longer accepted. All I'm saying is I would prefer if I could still pass callables, because it's more flexible and has nicer syntax, that's it.
from amp.
I also prefer to use callable, in amp v2, I have lots of code like call([$target, 'wakeup'])
, but in php8.1, first-class callable syntax is acceptable as an alternative async($target->wakeup(...))
.
In your example, a callable object implement __invoke is useful, but it's not commonly used.
class CallableThing {
public function __invoke()
{
}
}
$callableThing = new CallableThing();
$callableThing();
So, for now, I think you should accept first-class callable syntax or use closure fn().
from amp.
Related Issues (20)
- QUIC support HOT 2
- A concise way of resolving results that may turn out to be Promise, Generator or something else HOT 4
- An object reference sticks in the library HOT 1
- Running an async task in a synchronous application HOT 9
- stream_select hangs forever when FDs > 1024 are used HOT 5
- Getting access to child process data on SIGCHLD in Loop::onSignal. HOT 16
- Confusing diagram in README HOT 6
- Return self in Future::ignore HOT 1
- UnhandledFutureError is hard to debug HOT 2
- Migration guide for combinator functions HOT 5
- FiberError: Cannot switch fibers in current execution context on Laravel HOT 4
- 32 bit PHP: Return value of Amp\Loop\NativeDriver::now() must be of the type int, float returned HOT 5
- Any way to create or get a context for coroutine. HOT 5
- Version confusion HOT 8
- Shouldn't Future::finally() pass the future data to the provided callback? HOT 7
- Event loop terminated without resuming the current suspension (the cause is either a fiber deadlock, or an incorrectly unreferenced/canceled watcher) HOT 3
- Future combinators HOT 8
- When to not to use `async`?
- Comparison with ReactPHP HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from amp.