News for hackers. Stuff that matters.
Re:Python dicts are now ordered by skrebbel on Friday February 07, 2020 @01:48PM (#22269270)
Sure, PHP arrays are actually ordered maps, but it's super confusing when used as arrays. I mean, look at code like this:

    $arr = [];
    $arr[2] = "two";
    $arr[0] = "zero";
    $arr[1] = "one";
    echo join($arr, ", ");
    // two, zero, one
In any other language, the result of similar code would be "zero, one, two". You'd need to `ksort` this thing to get it to behave like a normal bog-standard boring array.

Everybody's writing PHP code pretending that it has arrays when really, it does not. The gotchas are way bigger than the gotchas in Python <3.7's unordered maps. It's a mess.

Re:Python dicts are now ordered by nedt on Monday February 10, 2020 @08:11AM (#22288777)
Why wouldn‘t you just push/append to the end of the array with '$arr[]'? There is no need for explicit keys in PHP. The gotcha is writing code in PHP pretending you are using a different language.
Re:Python dicts are now ordered by cdelsolar on Friday February 07, 2020 @03:32PM (#22270587)
I literally just learned "bog-standard" 10 minutes ago and now I see it here. And no, don't Baader-Meinhof me, I would have noticed this weird term in random text in the past.
Re:Python dicts are now ordered by viraptor on Friday February 07, 2020 @04:28PM (#22271154)
Would you though? Appears at least once a week: https://hn.algolia.com/?dateRange=all&page=0&prefix=false&qu...
Re:Python dicts are now ordered by coldtea on Friday February 07, 2020 @04:17PM (#22271062)
>I would have noticed this weird term in random text in the past.

Well, no so weird, I'm not a native english speaker, and I've seen the term hundreds of times over the years...

It's also quite common on HN:

https://www.google.com/search?q=site%3Anews.ycombinator.com+...

Re:Python dicts are now ordered by perl4ever on Friday February 07, 2020 @09:47PM (#22273455)
I'm aware of the terms "bog" and "bog-standard", but not being from the UK, I'm not sure what is so standard about bogs.
Re:Python dicts are now ordered by zepto on Friday February 07, 2020 @04:47PM (#22271334)
Baader-Meinhof denial!
Re:Python dicts are now ordered by sblom on Friday February 07, 2020 @06:06PM (#22272126)
Baader-Meinhof Dunning-Kruger?
Re:Python dicts are now ordered by tylerjwilk00 on Friday February 07, 2020 @02:33PM (#22269861)
I have to disagree also.

The order inserted makes most sense in most cases. Hand crafting integer based array indexes out of order is rare. And as you say a simple ksort call guarantees sort order.

    ksort($arr);
    echo join($arr, ", ");
    // one, two, three
Re:Python dicts are now ordered by dpacmittal on Friday February 07, 2020 @02:41PM (#22269985)
Exactly. Of you hand craft an array you probably need it in that order.
Re:Python dicts are now ordered by tylerjwilk00 on Friday February 07, 2020 @02:56PM (#22270171)
Then why not put it in order while you're hand crafting it?
Re:Python dicts are now ordered by kingdomcome50 on Friday February 07, 2020 @02:09PM (#22269560)
I disagree. The behavior you exhibit makes more sense than the behavior you imagine.

You start with an _empty_ array, then (somehow) set the third element of that array? That makes very little sense.

Had you _initialized_ your example as an array with a length of 3, your desired behavior would, indeed, manifest.

Re:Python dicts are now ordered by cliffy on Friday February 07, 2020 @02:48PM (#22270071)
The problem is PHP calling an ordered map an array. Array in pretty much every other language means a sequence indexed by integers.

Ideally the maintainers of PHP would rename it and deprecate the use of `array()` over a long period of time.

Re:Python dicts are now ordered by zeroimpl on Friday February 07, 2020 @09:19PM (#22273333)
The PHP documentation refers to them as "associative arrays", which is technically correct, but I agree I'm not sure how they could have conflated these concepts so badly.
Re:Python dicts are now ordered by tyingq on Saturday February 08, 2020 @01:12PM (#22277311)
I believe the "associative array" terminology came from Perl. Though Perl's associative arrays don't have any guaranteed order. And Perl also has normal arrays.
Re:Python dicts are now ordered by coldtea on Friday February 07, 2020 @04:21PM (#22271089)
>You start with an _empty_ array, then (somehow) set the third element of that array? That makes very little sense.

Well, there's no reason a language could not initialize an empty array to some initial capacity, either on declaration or when you add an indexed element.

Re:Python dicts are now ordered by kingdomcome50 on Monday February 10, 2020 @05:22PM (#22294057)
I guess my reasoning is that:

$arr = []

doesn't appear to be initializing to any capacity. So that after:

$arr[2] = "two"

I would expect:

count($arr) === 1

And honestly, sure. A language could be designed to pad the array with empty values if you specify an index greater than the upper bound (or lesser than the lower bound). I believe a sibling mentioned matlab did something like that.

But is that really better behavior or just different behavior? I don't really see an important difference. My point was that the criticism was fairly weak. PHP, indeed, has a very useful abstraction in its concept of "array". Most of the hate should be directed at the name, not the construct.

Re:Python dicts are now ordered by mattkrause on Friday February 07, 2020 @04:07PM (#22270954)
In that situation, Matlab allocates an array of length 3, fills it with “empty” values (depending on the type), and then sets the third element to the value. That’s what I’d have expected to happen here too...
Re:Python dicts are now ordered by zeroimpl on Friday February 07, 2020 @09:15PM (#22273304)
Would you expect it to do that if somebody wrote the following?

    $id = 12835151;
    $arr[$id] = Get_thing_with_id( $id );
Re:Python dicts are now ordered by mattkrause on Saturday February 08, 2020 @05:25PM (#22278943)
If $arr were an array I would, though I'd cringe at the wasted space (unless it were a sparse array).

Obviously, I'd agree that a map/associative array/dict/etc is a better choice here; I'm just annoyed about the name.

Re:Python dicts are now ordered by jrwr on Saturday February 08, 2020 @03:41AM (#22274746)
In my bog standard PHP I would do $thing = Get_thing_with_id( $id ); if(!empty($thing)){process($thing);}
Re:Python dicts are now ordered by jhasse on Saturday February 08, 2020 @02:44AM (#22274562)
yes.