hhvm / user-documentation

Documentation for those that use HHVM and write Hack code.
http://docs.hhvm.com/
Other
129 stars 159 forks source link

Advice on using list on vec is confusing #1367

Open qawt opened 6 months ago

qawt commented 6 months ago

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, 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.


lexidor commented 6 months ago

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 :)