feedback on Improved default constructors

58 views
Skip to first unread message

Seth Ladd

unread,
Oct 10, 2015, 3:23:44 PM10/10/15
to Dart Core Development
Hi,

Noticed that in the latest notes, there was a bit of discussion around "Improved default constructors" (https://groups.google.com/a/dartlang.org/forum/#!topic/core-dev/cQZlx90c7z0)

You might want to see how Flutter is being used, as it might provide some real-world examples of constructing nested object graphs. If there's ever a case for an improved syntax for constructing trees of objects, I'm not sure what is :)

For example:


      await showDialog(navigator, (NavigatorState navigator) {
        return new Dialog(
          title: new Text('Not Implemented'),
          content: new Text('This feature has not yet been implemented.'),
          actions: [
            new FlatButton(
              child: new Row([
                new Icon(
                  type: 'device/dvr',
                  size: 18
                ),
                new Container(
                  width: 8.0
                ),
                new Text('DUMP APP TO CONSOLE'),
              ]),
              onPressed: () { debugDumpApp(); }
            ),
            new FlatButton(
              child: new Text('OH WELL'),
              onPressed: () {
                navigator.pop(false);
              }
            ),
          ]


return new Material(
      type: MaterialType.canvas,
      child: new ScrollableViewport(
        child: new Container(
          padding: const EdgeDims.symmetric(vertical: 20.0),
          child: new BlockBody([
            new DrawerItem(
              icon: 'action/thumb_up',
              onPressed: () => _confirmOptimismChange(),
              child: new Row([
                new Flexible(child: new Text('Everything is awesome')),
                new Checkbox(
                  value: config.optimism == StockMode.optimistic,
                  onChanged: (_) => _confirmOptimismChange()
                ),
              ])
            ),
            new DrawerItem(
              icon: 'action/backup',
              onPressed: () { _handleBackupChanged(!(config.backup == BackupMode.enabled)); },
              child: new Row([
                new Flexible(child: new Text('Back up stock list to the cloud')),
                new Switch(
                  value: config.backup == BackupMode.enabled,
                  onChanged: _handleBackupChanged
                ),
              ])
            ),
          ])
        )
      )


Thanks for taking a look!

Seth

krupal shah

unread,
May 13, 2016, 11:46:02 AM5/13/16
to Dart Core Development
Sorry but this syntax of flutter looks like a callback hell.

Lasse R.H. Nielsen

unread,
May 13, 2016, 5:56:49 PM5/13/16
to Seth Ladd, Dart Core Development
Let's see if we can make that more readable.
First, let's remove "new" (and the unnecessary eta-expansion of debugDumpApp).

    return Dialog(
          title: Text('Not Implemented'),
          content: Text('This feature has not yet been implemented.'),
          actions: [
            FlatButton(
              child: Row([
                Icon(
                  type: 'device/dvr',
                  size: 18
                ),
                Container(
                  width: 8.0
                ),
                Text('DUMP APP TO CONSOLE'),
              ]),
              onPressed: debugDumpApp
            ),
            FlatButton(
              child: Text('OH WELL'),
              onPressed: () {
                navigator.pop(false);
              }
            ),
          ])

It's already more *readable*, but not really shorter (in height). It does give make it feel more natural to put some of the parameters on the same line. I guess there isn't much more to do with the available constructors - the remaining information is necessary.

More specialized constructors might allow some of the named parameters to be positional, it seems a little odd that all parameters are optional and named. I'd expect a FlatButton to always have a child and onPressed, so one or both of those could be a non-optional position parameter.

I'd expect something formatted like:

    return Dialog(
          title: Text('Not Implemented'),
          content: Text('This feature has not yet been implemented.'),
          actions: [
            FlatButton(Row([
                Icon(type: 'device/dvr', size: 18),
                Container(width: 8.0),
                Text('DUMP APP TO CONSOLE'),
              ]),
              onPressed: debugDumpApp
            ),
            FlatButton(Text('OH WELL'), onPressed: () {
              navigator.pop(false);
            }),
          ]);

I think that is already a very improved syntax, and it's just removing the "new" and reformatting a bit.
If our dart-formatter creates the mess above, then maybe it should be more compact - especially the "Container" argument shouldn't be on a line by its own. Maybe it's manually formatted for consistency, not readability (which can be, but isn't always, the same).

/L
-- 
Lasse R.H. Nielsen - l...@google.com  
'Faith without judgement merely degrades the spirit divine'
Google Denmark ApS - Frederiksborggade 20B, 1 sal - 1360 København K - Denmark - CVR nr. 28 86 69 84

Natalie Weizenbaum

unread,
May 13, 2016, 8:20:48 PM5/13/16
to Lasse R.H. Nielsen, Seth Ladd, Dart Core Development
Flutter doesn't use the formatter, but if they did, here's the sort of output they'd get.

--
You received this message because you are subscribed to the Google Groups "Dart Core Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to core-dev+unsubscribe@dartlang.org.

Lasse R.H. Nielsen

unread,
May 14, 2016, 6:33:02 AM5/14/16
to Natalie Weizenbaum, Seth Ladd, Dart Core Development
On Sat, May 14, 2016 at 2:20 AM, Natalie Weizenbaum <nw...@google.com> wrote:
Flutter doesn't use the formatter, but if they did, here's the sort of output they'd get.

Thanks. Yes, that's a lot more readable. Score one for formatter :)

/L
 

--
To unsubscribe from this group and stop receiving emails from it, send an email to core-dev+u...@dartlang.org.




--
Lasse R.H. Nielsen - l...@google.com  
'Faith without judgement merely degrades the spirit divine'
Google Denmark ApS - Frederiksborggade 20B, 1 sal - 1360 København K - Denmark - CVR nr. 28 86 69 84

--
To unsubscribe from this group and stop receiving emails from it, send an email to core-dev+u...@dartlang.org.

Reply all
Reply to author
Forward
0 new messages