hhvm / user-documentation

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

`$_` is not reserved for `list()` and `foreach` #1275

Closed fredemmott closed 2 years ago

fredemmott commented 2 years ago

It's frequently used for them, but is not reserved; it can be used for any mandatory-but-unused assignment.

facebook-github-bot commented 2 years ago

Hi @fredemmott!

Thank you for your pull request.

We require contributors to sign our Contributor License Agreement, and yours needs attention.

You currently have a record in our system, but the CLA is no longer valid, and will need to be resubmitted.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks!

fredemmott commented 2 years ago

👋 Slack's point of contact has emailed cla@ to add me to their corporate CLA

AndrewDiMola commented 2 years ago

@fredemmott - Was thinking about adding actual examples to make it more useful.

image

Do these examples convey what you're trying to teach?

$tuple = tuple('H', 'a', 'c', 'k', 'l', 'a', 'n', 'g');

// create names for $a, $b, $c, and $d, but ignore everything else
list($a, $b, $c, $d, $_, $_, $_, $_) = $tuple;
echo("I love ".$a.$b.$c.$d); // "I love Hack"
$keyed_container = dict['H' => 8, 'a' => 1, 'c' => 3, 'k' => 11];

// use the placeholder variable for keys instead of values
foreach ($keyed_container as $key => $_) {
  echo ($key); // prints keys: 'Hack'
}

echo(" ... and associated values: ");

// a regular example for contrast
foreach ($keyed_container as $key) {
  echo ($key); // prints values: 81311
}
fredemmott commented 2 years ago

The for-each example does; the tuple example technically does, however:

fredemmott commented 2 years ago

foreach ($keyed_container as $key) {

Also that should be $value, not $key

edwinsmith commented 2 years ago

Hi Fred!

facebook-github-bot commented 2 years ago

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

fredemmott commented 2 years ago

@AndrewDiMola would you like me to change these to full examples, or is that a follow-up you'd like to do?

what you're trying to teach

The main thing I want to do with this PR is to remove "is reserved for use in the list intrinsic function and the foreach statement"; just added some examples in passing while I was here to hopefully make 'why does this exist?' clearer :)

AndrewDiMola commented 2 years ago

@AndrewDiMola would you like me to change these to full examples, or is that a follow-up you'd like to do?

what you're trying to teach

The main thing I want to do with this PR is to remove "is reserved for use in the list intrinsic function and the foreach statement"; just added some examples in passing while I was here to hopefully make 'why does this exist?' clearer :)

We can merge and then I'll add improvements in a separate PR (maybe just for the foreach). Sorry for the delay! In Hack on Hack this week!