How much type-safety do we need for this PSR? #62
Replies: 1 comment
-
I wrote a benchmark for each of these 3 patterns, to compare them in terms of performance. https://github.com/mindplay-dk/provider-benchmarks It's a rather pointless benchmark. 😅 But let me explain: The It's difficult to say what you should conclude from that though - you could argue the point either way: if the optional interfaces are going to be optional, then why not include them. However, if they're going to be optional, favoring performance, most provider implementations would likely avoid using them, but then why include something you wouldn't want to use. In my personal opinion, it would be difficult to argue in favor of additional complexity, if it's just going to be complexity that, ideally, nobody uses, for performance reasons. Might as well not have that feature then. In addition, if a provider implementation wants extra type-safety at the expensive of performance, then there's nothing stopping them from adding these types themselves. If it doesn't buy us anything in terms of interoperability, again, difficult to argue in favor this. Lastly, the Now, this is just for a naive boilerplate wrapper-class around the However, as said, this is for a boilerplate wrapper-class around callables - which might not be how it's implemented in an actual DI container framework or a provider builder. It's not unthinkable you could do something safer/faster than using callables, so I wouldn't rule out this approach yet. Likely another test setup would be required to establish that. EDIT: I did give that a try, and found that even the most basic builder would need to use functions anyway, and ends up doing the same (or more) work anyway. If you wanted to do something more interesting than that, it's almost definitely going to involve fluent configuration or attributes, or something more "opinionated", any of which would add even more overhead. Unless you were to do something really complex, like compiling and caching the result, you would likely only add overhead - and if you were to go to those lengths anyway, it probably doesn't even matter what the underlying interoperability standard looks like anymore, since you'd be handling everything above the bare factory functions yourself anyway, and can do whatever you need or want in terms of both performance and safety. I would like to hear from someone who knows more details of how the Symfony container works, just to make sure we don't build something that's going to bottleneck a compiled container - but I don't really see how it could. Personally, I am usually very much in favor of making everything as type-safe as possible - but my current opinion in this context, is that this PSR should probably focus on bare bones interoperability. My view might be different, if PHP had function types, but it doesn't seem like extra type-safety is really going to buy us anything in terms of "low-level" interoperability between container and frameworks. |
Beta Was this translation helpful? Give feedback.
-
How much type-safety is required for this PSR?
Current Package
The current, published
container-interop/service-provider
package consists simply of this:Optional Callable Types
We could improve on the type-safety by introducing interface to describe the callables:
Note that this is just a formalization of the callable types - implementing these would be optional, and
callable
is still supported.Required Callable Types
Another option would be to have strict type-safety:
This way, the interfaces are required and must be implemented.
Discussion
The question is how much type-safety do we need?
To answer this question, keep in mind the purpose of this PSR, which in the current PSR draft is described as follows:
Some things to consider:
callable
?__invoke
in interfaces?These are just suggestions for things to consider and discuss - feel free to add more.
Beta Was this translation helpful? Give feedback.
All reactions