5 ms·
> array_is_list This is a stupid solution to a stupid problem. Defaulting to weak typing has enough problems, but changing the behavior of your data structure
by ashton314 6y ago
> array_is_list
This is a stupid solution to a stupid problem. Defaulting to weak typing has enough problems, but changing the behavior of your data structure based on its contents is next-level pain. I used PHP for a few years at a student dev job and I can’t count how many times this conflation wasted my time and introduced various bugs.
If PHP wants to become more respectable as a PL, it has got to break that out into real arrays (a linked list might be acceptable under certain circumstances) and maps.
- deleted 6y ago[deleted]
- FriedrichN 6y ago> I can’t count how many times this conflation wasted my time and introduced various bugs Could you give examples of this? I'm of the opinion PHP could use more strictly typed arrays/lists, but I haven't run into any issues with PHP arrays myself.
- Shish2k 6y agoThe one that annoyed me recently - numeric-looking string keys are turned into numeric keys. So if you have eg a hashmap of [naughty_word => replacement], then [“penis”=>”willy”] and [“asshole”=>”bottom”] work fine, but [“69”=>”cuddle”] crashes your program with an "unexpected integer” exception.
- FriedrichN 6y agoWhen using strict typing that is indeed a problem. Thanks for the example. edit: itchy trigger finger
- TazeTSchnitzel 6y ago> changing the behavior of your data structure based on its contents PHP does no such thing. There is one array type with one behaviour. There is no distinction between different kinds of arrays in the language.
- ashton314 6y agoDoch. Try piping the result of array_filter to array_map sometime. You can’t do it without an intermediate call to array_values first because the result of passing a list to array_filter does not behave like a list anymore: it operates strictly like a map and cannot have map called on it. (Oh the irony!)
- zerocrates 6y agoThe caveat I'm aware of with array_filter is that it can leave gaps in the indexes, but having an array like that doesn't break array_map. The output from it will just have the same index gap as the input.
- TazeTSchnitzel 6y agoYeah, there's nothing magical here: $ php -r 'var_dump([1, 2, 3, 4]);' array(4) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) } $ php -r 'var_dump(array_filter([1, 2, 3, 4], function ($x){return (bool)($x % 2);}));' array(2) { [0]=> int(1) [2]=> int(3) } $ php -r 'var_dump(array_map("strval", array_filter([1, 2, 3, 4], function ($x){return (bool)($x % 2);})));' array(2) { [0]=> string(1) "1" [2]=> string(1) "3" }
- deleted 6y ago[deleted]
- rbanffy 6y ago> If PHP wants to become more respectable as a PL Oh boy... That ship has sailed a long time ago...
- waheoo 6y agoDynamic typing is a trade off, one that requires some explicit type checking in some situations. It doesn't make it a stupid solution or a stupid problem. If you validate and conform your types at program edges it's rare you ever really need to do type checking. That is, a properly structured PHP application has all the benefits of dynamic typing and few of the problems that static type checking supposedly eliminates. I like both static and dynamically typed languages. Just like I like Async and non Async languages. All language types require practice and learning to adjust to the different patterns that are required. If you write PHP like JavaScript, or if you write PHP like java, or php like C++, you're going to have these problems. If you write PHP like PHP, function heavy, class light, and a mix of functional and procedural depending on context it actually works out very well for a lot of things. But go ahead, call the language stupid and irrespectable.
- ashton314 6y agoI like dynamic typing: most of my work is done in untyped Racket or Elixir. Weak typing is fun, but can be frustrating. My issue is that this single data structure functions like a list in some cases, and a map in others. Try piping the result of array_filter to array_map sometime. You can’t do it without an intermediate call to array_values first because the result of passing a list to array_filter does not behave like a list anymore: it operates strictly like a map and cannot have map called on it. (Oh the irony!)
- TazeTSchnitzel 6y agoarray_map works fine on the output of array_filter and produces a reasonable output in the same shape as the input.