You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.classTextPlusextendsText {
staticString _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);
staticStringdefData(String data) {
return data ==""? _defaultString : data;
}
staticTextStyledefStyle(TextStyle style) {
return style ??TextStyle(fontSize:40);
}
staticTextDirectiondefTextDirection(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:
// 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()classTextPlusextendsText {
// Main constructor@withRestOfOptionalTextPlus(
String data, {
TextStyle style,
TextDirection textDirection,
}) :superWithRestOfOptional(
data,
style:defStyle(style),
textDirection:defTextDirection(textDirection),
);
// methods to customize Text widgetstaticTextStyledefStyle(TextStyle style) {
return style ??TextStyle(fontSize:40);
}
staticTextDirectiondefTextDirection(TextDirection textDirection) {
return textDirection ??TextDirection.ltr;
}
}
Usage:
voidmain() {
// 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.
The text was updated successfully, but these errors were encountered:
Man, I'm so down with this. Glad you were able to find a way to put it into words. I agree, it is tedious to redefine variables that are simply getting passed upwards.
Introduce a convenience boolean constructor parameter called
withRestOfOptional
and a new super() constructor alternative calledsuperWithRestOfOptional()
. 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 ofText
, I would have to replicate all parameters ofText
.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:
Usage:
Benefits:
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.
The text was updated successfully, but these errors were encountered: