Add default
Keyword and New ::default
Operator for Using Default Parameters
#4096
Labels
feature
Proposed language feature that solves one or more problems
state-duplicate
This issue or pull request already exists
Add
default
Keyword and New::default
Operator for Using Default Parameters, with Hover DocumentationSummary
Introduce a
default
keyword and a shorthand::default
operator to Dart for explicitly invoking default parameter values in conditional expressions. Additionally, update the IDE hover documentation for constructors to indicate when a parameter is "defaultable," informing the user that they can usedefault
or::default
.Motivation
While Dart allows setting default values for optional parameters, there’s no intuitive way to invoke the default parameter value explicitly when using conditional logic. This forces developers to manually handle nullable values or repeat default values, leading to redundancy. Moreover, users are not informed directly in their IDE when parameters are defaultable, causing uncertainty.
Example Use Case:
Consider the following class:
Currently, when creating an instance conditionally, a developer would write:
The default value (
'123'
) is hardcoded in this logic, which is prone to errors and repetitive.Proposed Solution:
1. Introduce the
default
Keyword:The
default
keyword would allow developers to explicitly indicate the use of a default parameter value when the condition fails:2. Introduce the
::default
Operator:To make the code even more concise, introduce a
::default
operator. This operator would allow developers to pass parameters that fall back to the constructor-defined default value:articleCode
isnull
, the::default
operator uses the constructor's default value ('123'
), otherwise, it uses the passed value.3. Hover Documentation Indicating "Defaultable" Parameters:
To improve the user experience in IDEs, the constructor's parameter should be marked as defaultable in the hover documentation. This would inform developers when a parameter has a default value and that they can use the
default
keyword or::default
operator.Example:
When hovering over the
Article
constructor in an IDE, the following would appear:The tooltip or hover documentation could indicate:
This would provide immediate feedback, letting the developer know that the
articleCode
parameter can accept thedefault
or::default
keyword.Advantages of the Proposed Solution:
default
keyword and::default
operator reduce verbosity and eliminate the need for manually handling defaults.::default
operator makes it clear when default parameter values are being used, reducing the chances of errors or miscommunication in code.Example in Use:
Before:
After:
Using the
default
keyword:Using the
::default
operator:With hover documentation:
Current Workaround Without
::default
:In Dart's current state, without the
::default
operator, developers would need to manually handle default values with nullable checks:In this workaround, the
??
operator is used to fall back to the manually repeated default value ('123'
), but it lacks the convenience and clarity of the proposed solution.Conclusion:
By adding the
default
keyword, the::default
operator, and hover documentation indicating "defaultable" parameters, Dart will improve its handling of default parameter values. These changes will make code more concise, reduce manual error-prone handling, and offer a better developer experience by providing feedback directly in the IDE.The text was updated successfully, but these errors were encountered: