r/programming • • May 08 '19

Announcing Dart 2.3: Optimized for building user interfaces

https://medium.com/dartlang/announcing-dart-2-3-optimized-for-building-user-interfaces-e84919ca1dff
16 Upvotes

13 comments sorted by

3

u/inu-no-policemen May 08 '19

Collection-for and collection-if seem useful but also really unusual. Is there any other language which has something similar?

3

u/Darkglow666 May 08 '19

Languages like Kotlin have if expressions and such, and Dart's additions are kind of like that, but they only work within the context of collection literals.

15

u/munificent May 08 '19

Hi, I designed those. :)

They were inspired somewhat by Icon's notion of goal-directed execution. You can also think of them as another special kind of spread in that a spread produces zero or more values which get interpolated into the surrounding context.

The if syntax looks somewhat like languages where if is an expression, but that's actually not how they work here.

Loop comprehensions (especially as in Python) and Ceylon's comprehensions in argument lists also gave me good food for thought, especially the latter.

I don't know of any language that has these exact features, but I tried to pull from all of the above. I also spent a lot of time looking at existing Flutter and other Dart code to get a feel for where the pain points are and what changes would help.

2

u/murp11 May 09 '19

How do you think about the readability of those features? They support the deep nesting structures like the widget trees in flutter perfectly. IMHO these tree structures aren't so great to read even if they are reasonably sized. So these new features could easily be misused to lessen the readability. I really love flutter but it is so easy to write ugly code to create the widget tree.

1

u/munificent May 09 '19

How do you think about the readability of those features?

We did some usability testing, and people seem to read it pretty easily even when they aren't previously taught what it does. So I feel fairly confident that the new syntax itself is pretty readable, although I won't feel totally happy until after we've got real adoption and user feedback.

They support the deep nesting structures like the widget trees in flutter perfectly. IMHO these tree structures aren't so great to read even if they are reasonably sized.

Yeah, I share your concern. One of my goals with this work was to find way to just make those giant nested trees smaller and I wasn't really able to come up with anything to improve that. I had a few ideas, but they would have necessitated API changes in Flutter and the Flutter team, reasonably, didn't want to take that on.

I think there are a few approaches we can take to this:

  1. Build a simpler "convenience" API on top of Flutter that provides a simpler way to express common patterns like a column containing a list of widgets, etc.

  2. Make it easier — i.e. less syntax and boilerplate — to take a subtree of some widget and hoist it out to its own separate little widget. Especially with stateful widgets, that's a lot of effort, so people don't do it, so you get bigger more monolithic widget trees.

  3. Come up with some syntax that lets you express "modifier" widgets without nesting. Many widgets like Center technically wrap other widgets and have children, but the user doesn't conceptually think of it as a "parent" and the textual nesting might not add much value. If we can find a different way to write those, it might remove a lot of the nesting.

2

u/murp11 May 10 '19

Well after all it is like every language feature - it can be used for clean code and it can be abused for unreadable code. It really depends on the programmer. Especially the spread operator could be used to improve readability.

I think your second point is already possible. To prevent the huge monolithic trees I always split up the widget trees into reusable sub-widgets and if it is not feasible I extract the logic into a method. This is pretty easy but it isn't really enforced by flutter.

Your third point sounds very intersting. But it is also a key point of flutter that everything is a widget. I am curious how you and your team are going to handle this in the future.

2

u/airflow_matt May 10 '19

Thank you for all the work done on Dart. When I started with Flutter I was rather skeptical about the language, but it didn't take long to start seeing the benefits. I appreciate how pragmatic Dart is. When the type system landed, I was sold. That was a huge achievement and I can only imagine the effort it took to migrate everything.

I can't even tell you how eagerly I'm waiting for nnbd :) For me, this is the last big piece of the puzzle.

Well, I also still sometimes wish dart-fmt would let me control indentation, two spaces still bother me occasionally even after year of looking at it, but I guess I can't have everything :)

1

u/munificent May 10 '19

I can't even tell you how eagerly I'm waiting for nnbd :)

Me too! I've wanted it since before Dart 1.0.

3

u/Ameisen May 09 '19

How is Dart for writing scripts compared to Ruby, Python, or Crystal?

1

u/Darkglow666 May 09 '19

I use Dart for my quick scripts, and it's been great. Dart syntax is like C or Java, but less verbose, so people who know those kinds of languages can pick it up very easily compared to the alternate style of Python or Ruby.

2

u/IDCh May 09 '19

I would love to use Dart more, but it simply does not have enough libraries written in Dart itself or at least having type definition files like in TypeScript :( I tried to search a simple Tween library and did not find anything working except Flutter libraries, which are for mobile development.

I really, really like the api and standard library methods.

But it seems Dart within its relatively long lifespan has abandoned it's use as web language. I may be wrong, it's just it seems for me that is the case.

Does Google use Dart for it's web services, pages etc?

1

u/Darkglow666 May 09 '19

AngularDart is used a lot within Google. For the web, if something you need is missing a Dart implementation, you can just use JavaScript libraries quite easily. Also, Flutter is for mobile now, but it's being adapted for web and desktop, as well.

4

u/kankyo May 09 '19

If you want some more ideas for language features specifically for GUIs you should check out tri.table (python lib). I'm one of the authors.

We do some crazy stuff with python to get transparent layering of abstractions where you can tweak the underlying data structures very finely while keeping the simple case crazy simple. An example is to render an html table of a db model:

Table.render(request, model=User)

Now let's say I want to tweak that by changing the rendering of the header for username and hiding the password column:

Table.render(request, model=User, column__username__display_name='something', column__password__show=False)

there are much more advanced customization possibilities but I think this shows the basics. Everything has a name and a path so can be addressed.