{"action":"created","issue":{"url":"https://api.github.com/repos/facebook/hhvm/issues/6451","labels_url":"https://api.github.com/repos/facebook/hhvm/issues/6451/labels{/name}","comments_url":"https://api.github.com/repos/facebook/hhvm/issues/6451/comments","events_url":"https://api.github.com/repos/facebook/hhvm/issues/6451/events","html_url":"https://github.com/facebook/hhvm/issues/6451","id":114335255,"number":6451,"title":"Introduce vec<T> array type","user":{"login":"dlreeves","id":3945100,"avatar_url":"https://avatars.githubusercontent.com/u/3945100?v=3","gravatar_id":"","url":"https://api.github.com/users/dlreeves","html_url":"https://github.com/dlreeves","followers_url":"https://api.github.com/users/dlreeves/followers","following_url":"https://api.github.com/users/dlreeves/following{/other_user}","gists_url":"https://api.github.com/users/dlreeves/gists{/gist_id}","starred_url":"https://api.github.com/users/dlreeves/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dlreeves/subscriptions","organizations_url":"https://api.github.com/users/dlreeves/orgs","repos_url":"https://api.github.com/users/dlreeves/repos","events_url":"https://api.github.com/users/dlreeves/events{/privacy}","received_events_url":"https://api.github.com/users/dlreeves/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2015-10-30T19:06:11Z","updated_at":"2015-11-01T06:17:11Z","closed_at":null,"body":"This is a part of the [Hack array proposal](http://hhvm.com/blog/10649/improving-arrays-in-hack)\r\n\r\nA `vec` array is a container whose keys are zero-based continuous integers. This mirrors the behavior of the `Vector` class. This will be an array at runtime with extra restrictions to allow it to be used safely like a `Vector` 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.\r\n\r\n### Literal Constructor\r\n\r\nA `vec` is constructed using the literal expression `vec[]`.\r\n\r\n```hack\r\n$vec = vec[]; // Empty vec-array\r\n$vec = vec['a', 'b', 'c']; // vec-array of 3 elements\r\n```\r\n\r\nThis is a static expression, so it can be used in a static initializer.\r\n\r\n```hack\r\nclass C {\r\n  public static $vec = vec['a', 'b', 'c'];\r\n}\r\n```\r\n\r\n### Traversable Constructor\r\n\r\n A `vec` can also be constructed from any `Traversable` type, using the traversable constructor.\r\n\r\n```hack\r\n$array = array('a', 'b', 'c');\r\n$vec = vec($array); // constructs vec-array from another array\r\n```\r\n\r\n### Adding Elements\r\n\r\nTo preserve the property of having continuous integer keys new elements can only be added to a `vec` using the array append operator. \r\n\r\n```hack\r\n$vec = vec[];\r\n$vec[] = 1; // Allowed\r\n$vec[0] = 2; // Allowed\r\n$vec[10] = 3; // Error: 10 is out of bounds for the vec-array\r\n```\r\n\r\n### Removing Elements\r\n\r\nUsing unset on a `vec` type will demote it to the `dict` type. If you want to preserve the `vec` property then you will need to use the various array functions that operate at the beginning or end of an array such as `array_pop`, `array_push`, `array_shift`, `array_unshift`, etc.\r\n\r\n```hack\r\n$vec = vec[1, 2, 3, 4];\r\narray_pop($vec); // Now vec[1, 2, 3]\r\narray_shift($vec); // Now vec[2, 3]\r\nunset($vec[1]); // unsetting changes a vec to a dict[ 0 => 2]\r\n```"},"comment":{"url":"https://api.github.com/repos/facebook/hhvm/issues/comments/152797974","html_url":"https://github.com/facebook/hhvm/issues/6451#issuecomment-152797974","issue_url":"https://api.github.com/repos/facebook/hhvm/issues/6451","id":152797974,"user":{"login":"simonwelsh","id":125915,"avatar_url":"https://avatars.githubusercontent.com/u/125915?v=3","gravatar_id":"","url":"https://api.github.com/users/simonwelsh","html_url":"https://github.com/simonwelsh","followers_url":"https://api.github.com/users/simonwelsh/followers","following_url":"https://api.github.com/users/simonwelsh/following{/other_user}","gists_url":"https://api.github.com/users/simonwelsh/gists{/gist_id}","starred_url":"https://api.github.com/users/simonwelsh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/simonwelsh/subscriptions","organizations_url":"https://api.github.com/users/simonwelsh/orgs","repos_url":"https://api.github.com/users/simonwelsh/repos","events_url":"https://api.github.com/users/simonwelsh/events{/privacy}","received_events_url":"https://api.github.com/users/simonwelsh/received_events","type":"User","site_admin":false},"created_at":"2015-11-01T06:17:11Z","updated_at":"2015-11-01T06:17:11Z","body":"@Orvid that problem exists for anything using generics, it isn't specific to this, so isn't being brought into the language.\r\n\r\nThe actual type isn't known until it is passed through somewhere that makes it concrete (usually, an argument or return type)."}}