-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Proposal: Support [key: expr] alias for ['key' => expr] #6635
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
Closed
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ec3137b
to
3b5f033
Compare
Do you allow syntax mixing within one array declaration? |
Yes. This is mentioned in the description
|
In every place where `key` is a valid php identifier (e.g. can be used in PHP 8.0's named parameters), allow `[key: expr]` to be used instead of `['key' => expr]`. This is useful for shortening long arrays where the keys are known literals, e.g. ``` return [success: true, data: $data, cursor: $cursor]; // is equivalent to the following, but shorter: return ['success' => true, 'data' => $data, 'cursor' => $cursor]; ``` This uses a similar syntax to named parameter invocations, making it unlikely to cause future parser conflicts. ``` // Invoking a function with PHP 8.0 named parameters. process_api_result(success: true, data: $data); ``` This can also be used in the older `array()` value and `list()` destructuring syntaxes. Forbidding `key: value` there seemed like an unnecessary restriction that would make the language harder to remember and a language specification a bit longer. It is already possible to configure a linter to suggest that a project consistently use `[]` vs `array()` or `list()` according to coding standards, and automatically apply fixes. It is possible to mix syntaxes, e.g. this works: ```php return [ config: [ user: [id: get_user_id(), name: $name], ], myReference: &$this->property, $extraFieldName => $extra, ...$overrides, ]; ``` This was part of the potential future scope mentioned in https://wiki.php.net/rfc/named_params#shorthand_syntax_for_matching_parameter_and_variable_name ------- Many programming languages support the ability to use unquoted literals to concisely represent field names of data. Some examples are listed below: Javascript: literal keys are allowed. `{key: "value"}` is shorthand for `{"key": "value"}` and the former is frequently preferred. Python: `dict(key='value')` is equivalent to `{'key': 'value'}` and can be shorter for **long lists** of literal keys. (https://docs.python.org/3/tutorial/datastructures.html#dictionaries) (Note that {key: value} would be referring to the variable `key`, not a string literal, and key=value is the named parameter syntax) Ruby: `{key: 'value'}` is equivalent to `{:key => 'value'}` for Symbol keys https://docs.ruby-lang.org/en/2.0.0/Hash.html Lua: {key="value"} is equivalent to `{["key"]="value"}` https://www.lua.org/pil/3.6.html In compiled languages such as C++, Golang, and Rust, struct/classes are used more often than associative arrays for type safety, and identifiers are used unquoted in structs, and maps can use more types than just strings/ints so there generally isn't a dedicated shorthand. https://en.cppreference.com/w/c/language/struct_initialization https://doc.rust-lang.org/book/ch05-01-defining-structs.html https://tour.golang.org/moretypes/5
…ucturing)" This reverts commit 032dfc8.
3b5f033
to
3115e8c
Compare
@TysonAndre Are you planning on pursuing this RFC? |
Closing since there was no response. Feel free to reopen whenever work continues. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In every place where
key
is a valid php identifier(e.g. can be used in PHP 8.0's named parameters),
allow
[key: expr]
to be used instead of['key' => expr]
.This is useful for shortening long arrays where the keys are known literals,
e.g.
This uses a similar syntax to named parameter invocations,
making it unlikely to cause future parser conflicts.
This can also be used in the older
array()
value andlist()
destructuringsyntaxes. Forbidding
key: value
there seemed likean unnecessary restriction that would make
the language harder to remember and a language specification a bit longer.
It is already possible to configure a linter to suggest that a project
consistently use
[]
vsarray()
orlist()
according to coding standards,and automatically apply fixes.
It is possible to mix syntaxes, e.g. this works:
This was part of the potential future scope mentioned in
https://wiki.php.net/rfc/named_params#shorthand_syntax_for_matching_parameter_and_variable_name
After a quick check of mailing lists, this is about the same as the https://wiki.php.net/rfc/bare_name_array_literal which was proposed 6 years ago for php 5.x.
Because the named parameters RFC had passed and php has added many new syntaxes since then, and there are much better open source static analyzers available for php nowadays to catch potential issues, there may be more interest in adding this.
(Possibly in combination with allowing
[: $success, : $data]
as a shorthand for['success' => $success, 'data' => $data]
for array values and/or destructuring where variables have string names)The second commit also adds support for
[: $key] = $collection;
as an alias for['key' => $key] = $collection
and
return [: $key]
as an alias forreturn ['key' => $key]
. (plus reference syntax)That syntax is more readable and more efficient than extract/compact and saves a lot more typing
(https://wiki.php.net/rfc/named_params#shorthand_syntax_for_matching_parameter_and_variable_name)
EDIT: apparently, https://externals.io/message/101698 proposed that earlier with mixed, mostly negative responses
Many programming languages support the ability to use unquoted literals
to concisely represent field names of data. Some examples are listed below:
Javascript: literal keys are allowed.
{key: "value"}
is shorthand for{"key": "value"}
and the former is frequently preferred.Python:
dict(key='value')
is equivalent to{'key': 'value'}
and can beshorter for long lists of literal keys.
(https://docs.python.org/3/tutorial/datastructures.html#dictionaries)
(Note that {key: value} would be referring to the variable
key
, not a stringliteral, and key=value is the named parameter syntax)
Ruby:
{key: 'value'}
is equivalent to{:key => 'value'}
for Symbol keyshttps://docs.ruby-lang.org/en/2.0.0/Hash.html
Lua:
{key="value"}
is equivalent to{["key"]="value"}
https://www.lua.org/pil/3.6.html
In compiled languages such as C++, Golang, and Rust,
struct/classes are used more often than associative arrays for type safety,
and identifiers are used unquoted in structs,
and maps can use more types than just strings/ints so there generally isn't a
dedicated shorthand.
https://en.cppreference.com/w/c/language/struct_initialization
https://doc.rust-lang.org/book/ch05-01-defining-structs.html
https://tour.golang.org/moretypes/5