Introduce dict<Tk, Tv> array type #6452

Closed
dlreeves opened this Issue Oct 30, 2015 · 2 comments

Projects

None yet

5 participants

@dlreeves
Contributor

This is a part of the Hack array proposal

A dict array is a container mapping a valid arraykey to a value. This mirrors the behavior of the Map class. This will be an array at runtime with extra restrictions to allow it to be used safely like a Map from the type checkers perspective. This focuses mainly on the desired runtime behavior, but if there are questions for how it will interact with the type checker I'll be happy to discuss.

Literal Constructor

A dict is constructed using the literal expression dict[].

$dict = dict[]; // Empty dict-array
$dict = dict['a' => 1, 'b' => 2, 'c' => 3]; // dict-array of 3 elements

This is a static expression, so it can be used in a static initializer.

class C {
  public static $dict = dict[
    'a' => 1,
    'b' => 2,
    'c' => 3,
  ];
}

Traversable Constructor

A dict can also be constructed from any KeyedTraversable type, using the traversable constructor.

$array = array('a' => 1, 'b' => 2, 'c' => 3);
$dict = dict($array); // constructs dict-array from another array

Adding Elements

Appending an element to a dict will be a type checker error, but will still work at runtime. The array set operator is used to add a new element to the dict, similar to PHP arrays. However the key will not be coerced in any way. If the key is a string, then we will store a string and not convert it to an int if it is int-like.

$dict = dict[];
$dict[0] = 1; // Allowed
$dict['0'] = 2; // Allowed - does not override 0 key
$dict[] = 3; // Type Error

Removing Elements

Removing an element from a dict is identical to removing an element from a PHP array, namely by using unset.

@jwatzman jwatzman added the hack label Nov 8, 2015
@hhvm-bot hhvm-bot pushed a commit that referenced this issue Feb 29, 2016
@paulbiss paulbiss + Hhvm Bot Initial wiring for DictArray
Summary:Introduces a new array-kind for the DictArray.

See: http://hhvm.com/blog/10649/improving-arrays-in-hack
Related to #6452

Reviewed By: swtaarrs

Differential Revision: D2933163

fb-gh-sync-id: 1f28b217f7c33cdd45c26b984324b4529ea0a29b
shipit-source-id: 1f28b217f7c33cdd45c26b984324b4529ea0a29b
df33537
@hhvm-bot hhvm-bot pushed a commit that referenced this issue Feb 29, 2016
@paulbiss paulbiss + Hhvm Bot Runtime support for creating DictArrays
Summary:Introduces NewDictArray bytecode and associated IR instruction. Adds
support for creating dictionaries via ArrayInit and a static function on Array.

See: http://hhvm.com/blog/10649/improving-arrays-in-hack
Related to #6452

Reviewed By: swtaarrs

Differential Revision: D2933167

fb-gh-sync-id: 1b246cf6226ff1be865f1c63d16c1fcac03f7b03
shipit-source-id: 1b246cf6226ff1be865f1c63d16c1fcac03f7b03
d095b88
@hhvm-bot hhvm-bot pushed a commit that referenced this issue Feb 29, 2016
@paulbiss paulbiss + Hhvm Bot Serialize/Unserialize support for DictArray
Summary:Differentiate between arrays and dictionaries in the serialization
machinery. Also distinguish dictionaries in debug dumps by
printing dict rather than array.

See: http://hhvm.com/blog/10649/improving-arrays-in-hack
Related to #6452

Reviewed By: swtaarrs

Differential Revision: D2933170

fb-gh-sync-id: 7aa74b2784669b3a186d7e78faf0db8559c7219f
shipit-source-id: 7aa74b2784669b3a186d7e78faf0db8559c7219f
05ffe7e
@hhvm-bot hhvm-bot pushed a commit that referenced this issue Feb 29, 2016
@paulbiss paulbiss + Hhvm Bot Parser support for DictArray
Summary:Adds support for new dictionary literal syntax and support it for
static initialization as well as attributes. Makes dict a semireserved
word in hack and allows for functions named dict to exist.

See: http://hhvm.com/blog/10649/improving-arrays-in-hack
Related to #6452

  dict[$key => $value, ...]

Reviewed By: swtaarrs

Differential Revision: D2933175

fb-gh-sync-id: c43d8655117b85ab6e446c15269b655b9c766049
shipit-source-id: c43d8655117b85ab6e446c15269b655b9c766049
bff0a8f
@hhvm-bot hhvm-bot pushed a commit that referenced this issue Feb 29, 2016
@paulbiss paulbiss + Hhvm Bot Support for constructing DictArray from *Array
Summary:Introduced a builtin function `dict()` available in hack files. This
function will convert an array to a dictionary.

See: http://hhvm.com/blog/10649/improving-arrays-in-hack
Related to #6452

Reviewed By: swtaarrs

Differential Revision: D2933184

fb-gh-sync-id: e5358d4ee6c76c13b8e183dbbf3821d2ecada118
shipit-source-id: e5358d4ee6c76c13b8e183dbbf3821d2ecada118
b6b70d9
@hhvm-bot hhvm-bot pushed a commit that referenced this issue Feb 29, 2016
@paulbiss paulbiss + Hhvm Bot Runtime support for DictArray behavior
Summary:Runtime support for new DictArrat behavior.  The dictionary type does
not implicitly convert int-like string keys to ints nor does it silently coerce
invalid key types. For invalid keys (bool, float, object, etc) an exception
will be thrown.

Additionally attempting to read a key not contained in the array will throw an
exception rather than resulting in the usual warning/null return.

See: http://hhvm.com/blog/10649/improving-arrays-in-hack
Related to #6452
Related to #6454

Reviewed By: swtaarrs

Differential Revision: D2933186

fb-gh-sync-id: fbbd690adcac73748a48f18333101975770c1245
shipit-source-id: fbbd690adcac73748a48f18333101975770c1245
09239aa
@hhvm-bot hhvm-bot pushed a commit that referenced this issue Mar 1, 2016
@paulbiss paulbiss + Hhvm Bot JIT support for DictArray behavior
Summary:Adds support for new DictArray behavior to the JIT, see dependent diff
for details.

See: http://hhvm.com/blog/10649/improving-arrays-in-hack
Related to #6452
Related to #6454

Reviewed By: swtaarrs

Differential Revision: D2933192

fb-gh-sync-id: c8b6a04c26c77843ea30ec2927c5c5b00c102749
shipit-source-id: c8b6a04c26c77843ea30ec2927c5c5b00c102749
eb06dc9
@hhvm-bot hhvm-bot pushed a commit that referenced this issue Mar 1, 2016
@paulbiss paulbiss + Hhvm Bot Tests for DictArray
Summary:Breaking this out into a separate diff as it makes the stack more
reviewable, and because I'd like the hack team to have a look at the tests
to make sure that the behavior is as expected for this array kind.

See: http://hhvm.com/blog/10649/improving-arrays-in-hack
Related to #6452

Reviewed By: swtaarrs

Differential Revision: D2933196

fb-gh-sync-id: d40f34439364a388a045708952f5e6d96c94d1d8
shipit-source-id: d40f34439364a388a045708952f5e6d96c94d1d8
b0fb332
@raeesbhatti
raeesbhatti commented May 20, 2016 edited

This mirrors the behavior of the Map class.

Why does there need to be a dict array then? I mean, is there any special reasons to use this and not Map?

@simonwelsh
Contributor

It's an array, so has value semantics.

@Orvid Orvid closed this Sep 28, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment