-
Notifications
You must be signed in to change notification settings - Fork 11
Conversation
I worry this is out of preprocessor scope because you’d have to recognize the type of expression ->map() is called on, in order to recognize if it’s an array or not. |
I implemented something similar to this on native PHP using the scalar objects extension a few months ago. If you check my code, it just used the A third approach would be to just implement the ability to define scalar objects using Pre (implementing the aforementioned extension on Plus) and let people add their own array implementations. I can open another RFC for this if you want. |
The reason why I think this isn't suitable for a preprocessor is because guessing type of variable in PHP is very hard and unreliable. I know what I'm talking about - I wrote PHPStan :) Plus should stick to changing what's readable from AST, which leads to predictable behaviour. Even static analysis cannot always save you, if |
I think it could be extended to do what my package Coollection is able to do: access multidimensional collections (arrays) like we do with objects:
Instead of
Do
|
@ondrejmirtes Detecting arrays is certainly a hard task, but what if this feature only worked with variables that have a fixed array $foo = [1, 2, 3];
$foo->map(); // works
$bar = [1, 2, 3];
$bar->map(); // doesn't work |
You still need to do some nontrivial type inference. Imagine that |
The problem is that if the type inference fails and the code isn't transformed, the repercussions are really bad - you're calling a method on an array in runtime. |
fluent-arrays
Summary
Add a fluent API for working with arrays.
Motivation
PHP's native methods for working with arrays functionally (
array_map
,array_filter
,array_reduce
) are designed in a way that's consistent with C, rather than a way that's comfortable to write and read.For example,
array_filter
takes the array as the first argument and the callback as the second one, whilearray_map
takes the callback as the first argument and the array as the second one. This inconsistency makes understanding code hard.Also, using these methods in a fluent way is much more natural than e.g. mapping the result of filter, because you get to read the operations in the same order as they happen. First you filter, then you map.
Reference-level explanation
->
calls to arrays should be converted to calls to the respective low-level functions:An alternative implementation would be to wrap
->
calls to arrays in some global helper that would create an instance of a class like this:The benefit of this solution is that common methods like
sum()
,count()
, etc can be made part of that class (as long as it doesn't grow to the size of Collection). An interesting solution, that would work forsum()
andcount()
could beDrawbacks
If the
PHPPlusArr
implementation is chosen, we would need to safely attach->toArray()
at the end of the chain.Rationale and alternatives
An alternative to having this feature natively in the language/preprocessor is using something like Laravel Collections.
Unresolved questions
I'm not yet sure what's the best implementation for this. I'm also not sure how we can detect array variables correctly. Perhaps this should only work with variables that are strictly typed (something that could be provided by Plus) as
array
?