user-documentation icon indicating copy to clipboard operation
user-documentation copied to clipboard

Advice on using list on vec is confusing

Open qawt opened this issue 1 year ago • 1 comments

Please complete the information below:

Where is the problem?

https://docs.hhvm.com/hack/expressions-and-operators/list

"You may also use list() on a vec<T>, but it is not recommended."

What is the problem?

  1. It doesn't explain why, and what bad things could happen
  2. The example below literally does exactly that "foreach ($vec_of_tuples as list($first, $second, $third)) {" as an example.
  3. nit: "My personal favorite place" Who is this, and is this credible?

Please don't change anything below this point.


  • Build ID: HHVM=HHVM-4.164.0:HSL=v4.108.1:2024-02-08T13:44:46+0000:1fa47f258c6b68f8ec01899aa82fd6ffa0957109
  • Page requested: /hack/expressions-and-operators/list
  • Page requested at: Wed, 17 Apr 2024 20:37:50 +0000

qawt avatar Apr 17 '24 20:04 qawt

list($a, $b) on a tuple is always going to work. The typechecker knows that your tuple has two elements. When operating on a vec, the typechecker says, I'll trust you have checked the length somehow, since the length is not part of the type information.

The reason why using list on a vec is not recommended, is because people who are used to the tuple use case don't expect an OutOfBoundsException to be thrown from a list construct when the vec has fewer elements that the list construct has parts.

The example with the loop is not using list on the vec itself. It is using it on every element, which are tuples. Think list($a, $b) = $vec_of_tuples[0] instead of list($a, $b) = $vec_of_tuples.

As for your nit about "My personal favorite place", this was added in this PR by Lexidor.

I hope this helps :)

lexidor avatar Apr 18 '24 01:04 lexidor