Hi all,
I've been following the discussion on the Default Expression RFC, and while
I appreciate the effort to introduce more flexibility in handling default
values, I have some significant concerns about this proposal. I'd like to
share my thoughts and invite further discussion.
-
Complexity vs. Benefit:
The proposeddefault
expression adds considerable complexity to PHP's
syntax. While it might solve some edge cases, I'm not convinced the
benefits outweigh the added complexity. Our current methods (named
arguments, null coalescing, etc.) already handle most scenarios effectively. -
Principle of Least Astonishment:
PHP developers are used to default values being implicitly used when
arguments are omitted. Introducing an explicitdefault
keyword changes
this expectation and could lead to confusion, especially for newcomers to
the language. -
Type Safety and LSP:
The ability to modify default values at the call site raises concerns
about type safety. The RFC's example of potential LSP violations is
particularly worrying. This could lead to subtle bugs that are hard to
track down. -
Code Readability:
One of PHP's strengths is its readability. I'm concerned thatdefault
expressions, especially when combined with other operations, could make
code harder to understand at a glance. -
Performance Considerations:
The RFC mentions using reflection-like mechanisms fordefault
. Have we
considered the performance implications, especially for high-traffic
applications? -
Inconsistent Behavior:
The proposal suggests different behaviors fordefault
in various
contexts (e.g., function arguments vs. variadic args vs match expressions).
This inconsistency could be a source of bugs and confusion. -
Maintenance Burden:
Implementing and maintaining this feature, with all its proposed
restrictions and edge cases, seems like it would add a significant burden
to the language maintainers. -
Backward Compatibility:
While the RFC claims no known BC breaks, changing the meaning of
default
in certain contexts could potentially cause subtle issues in
existing codebases.
I understand the motivation behind this RFC, but I'm concerned that it's
solving a problem we don't really have, while introducing new ones. Perhaps
we could explore alternative ways to address the specific use cases that
motivated this proposal?
For example, the applyTheme
function in the proposal can be simply
implemented as:
function applyTheme(?string $theme = null) {
return new Config(...(isset($theme) ? [new $theme] : []));
}
This achieves the same result without having to call array_filter
or rely
on reflection, and without introducing a new language construct.
What do others think? Am I overlooking some key benefits that would justify
these concerns?
Looking forward to hearing your thoughts,
Cheers,
Hammed