Skip to content

[Proposal] Create a Dart class convenience constructor for optional parameters. #367

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

Open
wyattbiker opened this issue May 21, 2019 · 1 comment

Comments

@wyattbiker
Copy link

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.
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:

// 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:

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.

@ThinkDigitalSoftware
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants