Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Advice on using list on vec is confusing #1367

Open
qawt opened this issue Apr 17, 2024 · 1 comment
Open

Advice on using list on vec is confusing #1367

qawt opened this issue Apr 17, 2024 · 1 comment

Comments

@qawt
Copy link

qawt commented Apr 17, 2024

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.


  • 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
@lexidor
Copy link
Contributor

lexidor commented Apr 18, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants