# RFC: Allow trailing comma in closure use lists

- Status: Implemented (PHP 8.0)
- Authors: Tyson Andre tandre@php.net
- Date: 2020-07-01
- Version: 0.2
- Target: PHP 8.0
- Implementation: https://github.com/php/php-src/pull/5793
- Original: https://wiki.php.net/rfc/trailing_comma_in_closure_use_list
- Page: https://externals.io/rfc/trailing_comma_in_closure_use_list
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

## Votes

### Allow trailing comma in closure use lists (closed)

Yes: 49 · No: 0

Voters: alcaeus (Yes), asgrim (Yes), ashnazg (Yes), beberlei (Yes), brandon (Yes), brzuchal (Yes), bwoebi (Yes), carusogabriel (Yes), colinodell (Yes), cpriest (Yes), cschneid (Yes), dams (Yes), daverandom (Yes), derick (Yes), ekin (Yes), galvao (Yes), girgias (Yes), guilhermeblanco (Yes), ilutov (Yes), jasny (Yes), kalle (Yes), kguest (Yes), kocsismate (Yes), lcobucci (Yes), levim (Yes), marandall (Yes), marcio (Yes), mariano (Yes), mauricio (Yes), mcmic (Yes), nicolasgrekas (Yes), nikic (Yes), peehaa (Yes), petk (Yes), pmjones (Yes), pollita (Yes), ramsey (Yes), rdohms (Yes), reywob (Yes), salathe (Yes), sebastian (Yes), sergey (Yes), sirsnyder (Yes), svpernova09 (Yes), tandre (Yes), theodorejb (Yes), trowski (Yes), twosee (Yes), wyrihaximus (Yes)

## Discussions on the mailing list

- [Permitting trailing commas in closure use() declarations](https://externals.io/message/110715.md) — started on 2020-06-24, 3 messages
- [\[RFC\] Allow trailing comma in closure use list](https://externals.io/message/110804.md) — started on 2020-07-01, 1 messages
- [\[VOTE\] Allow trailing comma in closure use lists](https://externals.io/message/111018.md) — started on 2020-07-15, 2 messages

---

## Introduction

This RFC proposes to allow an optional trailing comma in closure use lists. This is already supported for argument and parameter lists.

Consider this example:

```php
$longArgs_longVars = function (
    $longArgument,
    $longerArgument,
    $muchLongerArgument,  // Trailing commas were allowed in parameter lists in PHP 8.0
) use (
    $longVar1,
    $longerVar2,
    $muchLongerVar3
) {
   // body
};
$longArgs_longVars(
    $longArgumentValue,
    $obj->longMethodCall(),
    $obj->longPropertyName ?? $longDefault,
);
```

This closure declaration has too many parameters and closure use variables to place all of them on one line, at least under conventional coding standards. In such cases [PSR-12](https://www.php-fig.org/psr/psr-12/#7-closures) recommends using the formatting above instead (PSR-12 predates support for trailing commas in parameter lists).

Unfortunately, it is currently not possible to place a trailing comma in the closure use list. This breaks uniformity, and results in larger diffs when a new optional closure use variable is added. Additionally, it is inconsistent with parameter lists and call-sites, which **do** allow a trailing comma.

At this point, I'm used to *always* adding a trailing comma to multi-line lists, regardless of what kind of element they contain. We should avoid having this kind of arbitrary restriction.

## Proposal

Allow a single optional trailing comma in closure use lists.

## Prior Art

[Allow trailing comma in parameter list](https://externals.io/rfc/trailing_comma_in_parameter_list) added support for trailing commas in parameter lists.

[Allow a trailing comma in function calls](https://externals.io/rfc/trailing-comma-function-calls) added support for trailing commas in calls.

[Trailing Commas In List Syntax](https://externals.io/rfc/list-syntax-trailing-commas) was a previous proposal to support optional trailing commas in all list-like structures.

Nothing has substantially changed since those RFCs in technical terms, but I think the aforementioned style for breaking up large lists has become more accepted in the meantime.

## Backward Incompatible Changes

None.

## Vote

Yes/No, requiring 2/3 majority.

Voting starts 2020-07-15 and ends 2020-07-29.

**Vote: Allow trailing comma in closure use lists** — Yes: 49 · No: 0 (closed)

## Changelog

0.2: Fix implementation link.

## Discussion

<https://externals.io/message/110715> “Permitting trailing commas in closure use() declarations”

<https://externals.io/message/110804> “\[RFC\] Allow trailing comma in closure use list”
