4 ms·
> I do think the argument for the check keyword instead of a ? operator like in Rust is quite weak. Mainly because a big advantage of the ? operator (apart from
by munificent 8y ago
> I do think the argument for the check keyword instead of a ? operator like in Rust is quite weak. Mainly because a big advantage of the ? operator (apart from the brevity) is that it can be used in method calling chains.
In Dart (as in a couple of other languages) we have an `await` keyword for asynchrony. It has the same problem you describe and it really is very painful. Code like this is not that uncommon:
await (await (await foo).bar).baz);
It sucks. I really wish the language team had gone with something postfix. For error-handling, I wouldn't be surprised if chaining like this was even more common.
- skybrian 8y agoWould it be better to use statements more? After all, this is a sequence of steps and it's doing a context switch between each step. That seems better written vertically, one per line.
- dom96 8y agoI'm not sure about Dart, but in my experience it's rare that you await a function which returns an object with another awaitable in it. Never mind something that nests these 2 levels deep.
- RodericDay 8y agofetch() + response.text() is usually two. add a user-defined third and you're there.
- reggieband 8y agoMaybe I'm crazy but I'd much prefer that as: const temp = await foo; const bar = await temp.bar; const baz = await bar.baz; But then again it looks like in this example we are returning promises from getters on objects which is something I would avoid.
- munificent 8y agoYeah, the guidance is usually to hoist the subexpressions out to variables like you do here. But I encounter it often enough that it feels like we are trying to paper over a poor language syntax. > we are returning promises from getters on objects which is something I would avoid. Getters are very common in Dart and it's idiomatic to use them even for properties that are asynchronous. (It's not like making it a method with an extra `()` really solves anything.) It's not as common to have nested chains of asynchronous properties, but they do happen, especially in tests.
- btilly 8y agoDeeply chained method calls are a code smell on their own. But would it really be that much better if you wrote it as something like: foo..bar..baz;