News for hackers. Stuff that matters.
Re:PHP 7's new hashtable implementation by adunn on Tuesday December 23, 2014 @03:00AM (#8787251)
Well, yeah, I agree that the SPL types are nice to have. My complaint is that PHP's only first-class array type is a weird array/map combo. Have you ever seen anything like that in another language?

The SPL types are definitely a welcome addition to the language, but they feel like add-ons. Definitely not first-class. The standard array functions don't work with SPL types (array_map, etc.) even though the SPL types are iterable.

More to my point, missing from SPL is a dynamically-sized array that's not based on linked lists. Linked lists don't have O(1) lookup. This type of array really should be first-class, but it’s completely missing from the language. Please correct me if I’m overlooking something!

That said, I believe having separate, first-class, dynamic arrays and maps would strike a better balance of dynamism, performance, and predictability compared to the single existing first-class array/map.

Re:PHP 7's new hashtable implementation by Elv13 on Tuesday December 23, 2014 @03:18AM (#8787284)
Lua also have hybrid array, maps and object. It does work well when implemented in a sane way. Lua has different way of iterating the table if you want an array or maps.
Re:PHP 7's new hashtable implementation by stdbrouw on Tuesday December 23, 2014 @04:06AM (#8787375)
... though I wouldn't necessarily call Lua's way of doing things "sane". E.g. if Lua encounters a `nil` somewhere in your table, it will stop iterating.
Re:PHP 7's new hashtable implementation by joeyo on Tuesday December 23, 2014 @05:07AM (#8787520)
It's certainly sane in the sense that it was an intentional, if perhaps unusual, design decision. I personally like it since it results in an efficient implementation of sparse arrays.

In any event, Lua 5.2 will respect the __len, __pairs and __ipairs metamethods, so you can tweak this behavior if you need to store nils in your tables and iterate over them.

Re:PHP 7's new hashtable implementation by stdbrouw on Tuesday December 23, 2014 @07:28AM (#8787860)
The world would be a beautiful place if everything intentional were sane :-) It's bitten me a couple of times (e.g. when unpacking arguments in a function to feed them to another function, where one of the arguments is nil) but I can see how it could be the desired behavior in some cases.
Re:PHP 7's new hashtable implementation by brazzy on Tuesday December 23, 2014 @03:29AM (#8787296)
> My complaint is that PHP's only first-class array type is a weird array/map combo. Have you ever seen anything like that in another language?

Um, Javascript?

Re:PHP 7's new hashtable implementation by shimonamit on Tuesday December 23, 2014 @03:43AM (#8787335)
Javascript's arrays cannot be used as maps or associative arrays. Arrays require numeric, consecutive keys. Otherwise it's an Object.

--

Clarification edit: Creating a new key on an array using arr['key']=1 creates a property on the arr Object but does not add an element to the standard array.

http://stackoverflow.com/questions/8630471/strings-as-keys-o...

Re:PHP 7's new hashtable implementation by mc808 on Tuesday December 23, 2014 @04:01AM (#8787366)
Arrays are always objects in JavaScript, and they can be extended like any other object or used as maps (barring key conflicts).
Re:PHP 7's new hashtable implementation by dsp1234 on Tuesday December 23, 2014 @04:46AM (#8787486)
Adding non-numeric keys does not remove an Array's "arrayness" in JavaScript

  var x = [];
  console.log(Object.prototype.toString.call(x));
  //[object Array]
  
  x[0] = 1;
  console.log(x[0]);
  //1
  
  x["test"] = 2;
  console.log(x["test"]);
  //2
  
  console.log(Object.prototype.toString.call(x));
  //[object Array]
  
  x.map
  //function map() { [native code] }

  var y = {};
  
  console.log(Object.prototype.toString.call(y));
  //[object Object]
  
  y.map
  //undefined
Re:PHP 7's new hashtable implementation by nothrabannosir on Tuesday December 23, 2014 @09:14AM (#8788150)
His point was:

    x = [1]; x["test"] = 555; x.map(function (y) { return y; });
    // [1]
That's [1], not [1, 555]. You can access the array from the map interface, but not the other way around.
Re:PHP 7's new hashtable implementation by dsp1234 on Tuesday December 23, 2014 @01:14PM (#8789298)
The array prototype functions are only specified by the standard to deal solely with numeric keys, but the lines between and object and array are still pretty blurry. The use case for each is different, but the original question far above was "array/map combo. Have you ever seen anything like that in another language?", and clearly JavaScript is very similar even if not exact.

  var x = {};
  x[0] = 1;
  x.length = 1;
  x.map = Array.prototype.map;
  x.map(function(y) {return y;});
  //[1]
Re:PHP 7's new hashtable implementation by the_mitsuhiko on Tuesday December 23, 2014 @05:40AM (#8787597)
It does internally. Pretty sure that's even defined in the standard.
Re:PHP 7's new hashtable implementation by gsnedders on Tuesday December 23, 2014 @07:53PM (#8791030)
No, it doesn't. The only magic per spec is about the "length" property (which is essentially a getter/setter pair, despite being a data property and not an accessor pair). That's the only thing special about arrays in JS.
Re:PHP 7's new hashtable implementation by cgrand-net on Tuesday December 23, 2014 @06:25AM (#8787726)
If you can't observe a difference, does it matter?
Re:PHP 7's new hashtable implementation by robocat on Tuesday December 23, 2014 @05:07AM (#8787518)
Arrays are full objects e.g. function(){ var x = []; x.foo = "bar"; return x.foo;} returns "bar".

That is the whole reason prototype.js library broke the use of for (var x in somearray) because the library set properties on Array.prototype.

Re:PHP 7's new hashtable implementation by aikah on Tuesday December 23, 2014 @06:28AM (#8787731)
Nope.

Array are objects,but all objects aren't Arrays.

    var a={};

    a instanceof Array // false
Furthermore,Javascript objects aren't maps.