dart-lang / dart-lang/language
[Proposal] Create a Dart class convenience constructor for optional parameters.
- Dominant language
- TeX
- Stars
- 2.9k
- Forks
- 239
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 14
Description
Introduce a convenience boolean constructor parameter called `withRestOfOptional` and a new super() constructor alternative called `superWithRestOfOptional()`. This would allow using the optional parameters, such as those found with Flutter, without having to specify them in the subclass constructor, unless overridden.
Currently this is how it works. If I were to extend a class such as `Text`, such that my user has access to all parameters of `Text`, I would have to replicate all parameters of `Text`.
```dart
import 'package:flutter/material.dart';
// Currently: Have to repeat all the protocol of superclass, in the superclass constructor
// if I want to extend and enhance class for the user.
class TextPlus extends Text {
static String _defaultString = "fill this in";
TextPlus(String data,
{TextStyle style,
StrutStyle strutStyle,
TextAlign textAlign,
TextDirection textDirection,
Locale locale,
bool softWrap,
TextOverflow overflow,
double textScaleFactor,
int maxLines,
String semanticsLabel})
: super(defData(data),
strutStyle: strutStyle,
style: defStyle(style),
textAlign: textAlign,
textDirection: defTextDirection(textDirection),
locale: locale,
softWrap: softWrap,
overflow: overflow,
textScaleFactor: textScaleFactor,
maxLines: maxLines,
semanticsLabel: semanticsLabel);
static String defData(String data) {
return data == "" ? _defaultString : data;
}
static TextStyle defStyle(TextStyle style) {
return style ?? TextStyle(fontSize: 40);
}
static TextDirection defTextDirection(TextDirection textDirection) {
return textDirection ?? TextDirection.ltr;
}
}
```
**Preferred:**
Add an optional boolean constructor parameter, that allows all the parameters to be passed as defaults if not overridden. So in this example have the same subclass but with fewer declarations. See example:
```dart
// In this case all I need is to define the enhancements. The rest of the Constructor
// should assume the optional parameters, and passed all the way down the
// line using superWithOptionalParams()
class TextPlus extends Text {
// Main constructor
@withRestOfOptional
TextPlus(
String data, {
TextStyle style,
TextDirection textDirection,
}) : superWithRestOfOptional(
data,
style: defStyle(style),
textDirection: defTextDirection(textDirection),
);
// methods to customize Text widget
static TextStyle defStyle(TextStyle style) {
return style ?? TextStyle(fontSize: 40);
}
static TextDirection defTextDirection(TextDirection textDirection) {
return textDirection ?? TextDirection.ltr;
}
}
```
**Usage:**
```dart
void main() {
// Notice softwrap did not need to be specified in the proposed subclass style as an optional parameter
// It is just passed thru.
var anObj1 = TextPlus("some string", softWrap: true, style: TextStyle(fontSize: 30));
// Notice textalign did not need to be specified in the proposed subclass style as optional parameter.
// It is just passed thru.
var anObj2 = TextPlus("", softWrap: true, textAlign: TextAlign.left, textDirection: TextDirection.ltr);
}
```
**Benefits:**
- Shortens Subclass Code.
- Saves time creating subclass.
- Easy to Refactor / Extend a class without having to replicate complete protocol.
**Caveats:**
I expect there maybe issues with my proposed syntax. The idea is to develop an easy way to extend class constructors, without the need of specifying the complete protocol of the parent.
Contributor guide
Assessment
This issue has not been assessed yet.