8 ms·
Flutter: Futures, Isolates, Event Loop
- zdragnar 8y agoI'm no functional fanatic, but one thing that I enjoy about it is the idea that function calls- and futures- resolve to a value. The code example where D doesn't get called last because of the missing await grinds my gears something fierce, especially because I missed it the first glance through. This type of bug is much harder to create when you don't constantly rely on side effects everywhere. That said, the article did a really good job if you're not already familiar with the same concepts from JS land (with the event loop sans visible microtasks, Promise instead of Future, abd web worker instead of Isolate).
- Taig 8y agoThis is my main point of criticism about Dart (and Flutter SDK design). While Flutter is incredibly pleasant to work with compared to native Android or iOS SDKs, it still feels so terribly ill-conceived in so many areas coming from a functional background. For the Dart part the Future-problems you already mentioned and the need for async/await language level keywords is disappointing. Also if/else not being an expression as well as syntactic complexity (e.g. defining constructors with factory/static/const keyword combinations). Why come up with a new language in 2011 that repeats the null mistake? Flutter gives you a beautiful and comprehensive set of widgets and makes a great first impression. But when digging a bit deeper, things tend to get ugly. Massive side effects hidden deep inside OOP structures (e.g. routing), stuff like mixins, validation tied into view layer etc. I couldn't help but find so many mistakes being repeated. After spending a couple months with Flutter, I decided to ditch mobile development all together. I wish they followed the direction react is steadily going: promoting simple, pure widgets and passing values around, avoiding side effects.
- munificent 8y ago> the need for async/await language level keywords is disappointing This is unfortunate [1], I agree. The challenge is that if you don't do that, it's very hard to compile the resulting code to efficient JavaScript. Dart was initially only a web language and is now both a web and a mobile language. You can think of function calls as having two different calling conventions: return the value normally, or return it by invoking a continuation with the result. Asynchronous functions require the latter. You could compile all function calls to that style, but it's much slower and generates larger JS. You could try to infer which functions should be called using an async style and which shouldn't. That works in simple cases, but breaks down in complex cases involving generics and higher-order functions. I don't know of any literature that investigates how feasible this is in practice, so making your language rely on this optimization for tolerable performance is a very large gamble. This may change over time now that JS has generators and may be adding async/await syntax, but Dart was designed before either of those existed. > Also if/else not being an expression Yeah, everything being an expression is nice. I wish Dart had been designed that way. The original designers of the language believed they needed to be very conservative in order to be successful and I think they overshot that mark. > syntactic complexity (e.g. defining constructors with factory/static/const keyword combinations) Some of this is annoying, yes. It does have some practical value, though: * The restriction around initializing final fields in the constructor initialization list, which is what forces you to use factory constructors in some cases, ensures a very nice property: it means in Dart you can never observe a final field before it has been initialized. This is not true in C#, Java, or Kotlin. Now that we are working on non-nullable types for Dart [2], this will let us have sound non-nullable types, unlike those other languages. * Personally, I find const to be more of a hassle than it's worth, but const constructors do let you define real compile-time constant objects of user-defined classes, which isn't something some other languages can express. That, in turn, lets you rely on very fast identity tests for checking equality between two objects. Flutter relies on this heavily when diffing the widget tree to determine which parts have changed efficiently. > Why come up with a new language in 2011 that repeats the null mistake? Tell me about it [3]. (Note the date on that blog post.) We're working to fix it now. [1]: http://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/ http://journal.stuffwithstuff.com/2015/02/01/what-color-is-y... [2]: https://github.com/dart-lang/language/issues/110 https://github.com/dart-lang/language/issues/110 [3]: http://journal.stuffwithstuff.com/2011/10/29/a-proposal-for-null-safety-in-dart/ http://journal.stuffwithstuff.com/2011/10/29/a-proposal-for-...
- matt2000 8y agoThis is a great writeup, wish I had it when I started with Flutter. BTW, I've written a couple Flutter apps now and this set of tools (Futures, async/await, Isolates) covers my multithreading use cases in a really nice and safe way. By comparison, Grand central dispatch is pretty nice on iOS, but I really miss the first class async/await calls. Side note: How did they make an entirely new language for app development (Swift) and not build in async/await support? Kotlin co-routines seem to be a pretty nice solution on Android, but before that it was a bit of a mess. There was 4 or 5 different ways to do things async, they all interact in super complicated ways with the activity life cycle, and basically nothing is quite what you need. Side side note: Unity also does a really nice version of all of this with a single event loop and really powerful co-routines. Worth checking out sometime.
- foota 8y agoI'm not sure async await was as much in the limelight at the start of development on swift, but I could be wrong.
- ygra 8y agoIt's been part of F# since 2007 and C# since 2013, so the feature was probably known at design time of the language. However, language design is always a complicated dance of trade-offs, so there's never a way to initially account for all features that are eventually desired and implemented.
- guzik 8y agoSwift 5 will most likely include async/await
- Apocryphon 8y agoWhenever that is. It was originally slated for the last year.
- coldtea 8y ago
- pests 8y agoI've been getting into Flutter and Dart recently after being a long time React fan. I must say I like many things Flutter does differently. Especially around its Material UI package (built-in) which is miles above any other web material component library in any framework (React, Vue, Angular, etc.) I must say wrapping my head around the addition of 'await' and the changes it made to the EventLoop processing was difficult but as I type this comment I realize I do fully grok it now. I must say that this is IMO probably the most important piece of info in the article. Basically, "`await` causes immediate execution of the Future (up to the first `await` in the Future) verses appending the Future to the end of the EventLoop queue" If I have the above wrong someone please call me out. Great article.
- tylerhou 8y agoAwait doesn’t immediately execute the future; see the example code in the article. It looks like the Dart concurrency model is basically the same as JS except with microtasks and replacing “worker” with “isolate.”
- pests 8y agoI'm not sure I agree. In the first example `await` is added before the Future creation and this causes immediate execution in the printed output (second version). In the first version both Futures are pushed onto the EventTask queue and are executed after main() has finished. In the second example with method1 and method2, in method1 with async (which signals a Future) on the forEach, all three Futures are pushed to the EventLoop queue and execute after method1() has finished. In method2() there is no new Future being added and execution happens immediately with the end of method2 executing after all delayedPrints. You also see the effect with all three Futures resolving at once in the method1 case due to all three being added to the queue at the same time each with the same delay. Whereas with method2 each print with a delay after due to this function being synchronous with nothing going on the EventLoop.
- tylerhou 8y agoAwait pushes your task onto the end of the event queue; this might seem like you’re immediately executing a future in many cases but it’s not true in all cases. Other futures created by your thread might take priority. Consider the case where you pushed a future onto the event loop which infinite loops before you used await. Then your code will freeze.
- huhtenberg 8y agoA typo: while (microTaskQueue.isNotEmpty){ ... return; } It should be either "if { ... return }" or just "while { ... }".
- ridiculous_fish 8y agoWhat is the role of the `async` keyword in Dart? I couldn't understand it from the article. > the outcome of the method is a Future Isn't the function signature sufficient to express this? > it runs synchronously the code of that method up to the very first await keyword How is this different from non-async methods? > the next line of code will be run as soon as the Future, referenced by the await keyword, will have completed But the await keyword is in the caller which is not statically known. What is the difference between an async method and a method that returns a Future?
- pxndx 8y ago> What is the difference between an async method and a method that returns a Future? There's no difference for callers of the method, but you can only use await in methods that are async.
- hbhakhra 8y agoAn async method can use await within it. A method can return a future but not be able to use the await keyword.
- ridiculous_fish 8y agoWhat is the point of this restriction? Why not just allow all Future-returning functions to use await?
- qqqwww 8y agoAsync methods also allow you to return values which will be returned as futures. For example: return 4; If the function is marked async, the return value should be Future<int>. If you allow a non-async method use await, you get into this situation: Future<int> foo(){ int result = await something (); // How to return "result" and still make compiler happy? }
- ridiculous_fish 8y ago
- ymannn 8y agoWow, I have no idea how similar Dart is to JavaScript. Isolate == Worker Future == Promise
- inquisitiveio 8y agoThey touch on the importance of compiling to efficient JavaScript in their FAQ https://www.dartlang.org/faq#q-why-isnt-dart-more-like-haskell--smalltalk--python--scala--other-language https://www.dartlang.org/faq#q-why-isnt-dart-more-like-haske... But it probably also helps that one of dart's creators, Lars Bak also developed the V8 JavaScript engine.
- kamura 8y agoFor someone used to work with async/await only in C#, I was not expecting the execution model to be so fundamentally different in Dart. Something to try to wrap my head around later!
- vmsp 8y agoIf you're interested in the Dart VM, I also recommend taking a look at Vyacheslav Egorov's Introduction to Dart VM (https://mrale.ph/dartvm/ https://mrale.ph/dartvm/).