4 ms·
"New array_is_list function" I use PHP a fair amount, but this is one area that irks me. PHP arrays can be arrays, lists, and hash maps. PHP obviously copied
by tyingq 6y ago
"New array_is_list function"
I use PHP a fair amount, but this is one area that irks me. PHP arrays can be arrays, lists, and hash maps. PHP obviously copied some of Perl's features, and I never understood why hashmaps and arrays weren't different types, as they are in Perl.
- nkozyra 6y agoI haven't used PHP with regularity since the start of 7.0, but I agree. Having this mushy collection type seems so easy to just replace with [] for arrays/lists and {} for objects/class instantiations by convention.
- thibran 6y agoFunny. I almost dislike all of PHP but think that their choice of basic data structure is the one good thing. Computers are getting faster and faster and simplifying programming could be done to a whole new level. Clojure for example has a similar approach. Okay, PHP lacks contracts, but maybe PHP adds them in 20 years when the mainstream understands that they are great.
- kitsune_ 6y agoClojure's persistent collection types include List, Vector, Map, Set and you have namespaced keywords. In general, I don't see how you can compare the approach of Rich Hickey and Clojure to PHP to be honest...
- __jem 6y agoAlso, those collection types are backed by a number of concrete implementations.
- ashton314 6y agoI too dislike PHP, but this is one of the things I hate the most: this is like conflating addition and multiplication of types. Arrays and maps have separate use cases and very different trade-offs and PHP gives you no choice in the matter.
- tottenhm 6y agoIn fairness, it does give choices -- https://www.php.net/manual/en/spl.datastructures.php https://www.php.net/manual/en/spl.datastructures.php
- duskwuff 6y agoThe SPL data structures are widely considered a mistake. They're awkwardly designed, cannot be type-hinted, and often perform worse than pure-PHP alternatives.
- duckerude 6y agoFor me it didn't really simplify programming, it just added mental overhead. Every array I used was either strictly sequential or strictly a mapping, and the language did nothing to help me keep track of that. It also makes some potential niceties impossible. Python can give a special meaning to a negative index into a list, because it can never truly exist, but PHP has to treat it like a key and do a normal lookup.
- otabdeveloper4 6y agoComputers aren't getting faster. In fact, they're getting slower in real practice. (We've been trading off speed for safety and power consumption for years now.)
- pornel 6y agoI love them. Lisp can use lists for everything, and PHP arrays ;) Thanks to their mixed nature they're very versatile. People even make small DSLs out of them. And they're order-preserving, so they won't inject non-determinism just to smugly teach you a lesson about real hash tables.
- tmpz22 6y agoShots fired at Golang
- Blikkentrekker 6y agoAlmost anything can be simulated in any other data type, often with poor performance and easy bugs. If a programmer use lists in lisps where vectors or hash tables are more appropriate, he is doing something wrong. Likewise, simulating arrays in hash tables, which is what PHP expects one to do, is a very bad idea. Bash lacks multidimensional arrays, but it does have hash tables, so some programmers resort to simulating multidimensional arrays by using keys such as `"3,4"` as a string in hash tables, this would be a very bad idea if Bash had true multidimensional arrays. In it's lack thereof, it is only a bad idea where no better idea is possible.
- raspasov 6y agoThe reality of most of programming I've done is that in 98% of cases you deal with data structures that are sufficiently small for performance not to matter much on modern hardware. If you do need to deal with data structures that are quite large (i.e., take GBs of memory), perhaps PHP is not the right language.
- Blikkentrekker 6y agoThe performance is hardly the biggest issue, the ease of bugs is.
- eyelidlessness 6y agoThe problem is that lisp APIs tend to treat paired sequences as equal citizens, whereas PHP treats paired arrays as a special case and they're effectively a different data type. This is the same problem with how Erlang/BEAM languages treat strings as a list of character data. It's the same until it's not, and then it's decidedly not.
- krick 6y agoI'm kinda used to PHP "arrays", but I really wish it had proper lists. This array_is_list function doesn't change things much: I already have the same implemented in many of my projects. The point is that I shouldn't do that: I just should use a typehint the same way as I'd do it with array or MyCustomClass. Of course there is Ds\Sequence, but having it as a part of some relatively obscure library isn't the same as having it as a part of a language, with syntax as simple as [1,2,3]. Actually, I'm not even sure: does it have as good performance, as "arrays" do? I never tested.
- duskwuff 6y agoWhat `array_is_list()` changes is that it makes this check fast. Previously, the fastest method of checking for a list-like array was `$arr == array_values($arr)`, which was slow on large lists and could fail entirely on some unusual structures containing circular references. (I was involved in the development of this function: https://github.com/php/php-src/pull/4886 https://github.com/php/php-src/pull/4886)
- tyingq 6y agoIs array_is_map(), etc, planned? Or is the pure php equivalent fast enough for those?
- duskwuff 6y agoYou mean `!array_is_list()`? As far as PHP is concerned, a list is a subset of arrays where the keys are ordered integers starting at zero. Any array that doesn't fit this pattern could be considered a "map" -- there's no middle ground.
- edoceo 6y agoIt's simpel! Name your vars $some_list for integer arrays and $some_hashmap (sometimes $some_coll (collection)). I'm 1/2 joking. I maintain loads of legacy and recent PHP LOB apps. Var-name fixes can do a lot to help the case in any loosely typed langs (I learned the trick from VB4.0)
- pwdisswordfish6 6y agoWhen a type system is lacking, Hungarian notation will have to do.
- superasn 6y agoYes a million times this. This becomes a even bigger problem with json_encode when PHP can't tell the difference between an object and array sometimes and outputs the wrong type. Yes you can use stdClass but that feels like such a hack. It would have been amazing if they were different types all along.