Skip to content

Instantly share code, notes, and snippets.

@mono0926
Last active August 9, 2021 04:20
Show Gist options
  • Save mono0926/ad031466d1ee8b9632657226aa5d6c46 to your computer and use it in GitHub Desktop.
Save mono0926/ad031466d1ee8b9632657226aa5d6c46 to your computer and use it in GitHub Desktop.
Dart言語のカスケード記法クイズ
final x = [3, 5, 1];
// それぞれ行の返却値は?
x..sort()..map((v) => v * 2);
x..sort().map((v) => v * 2);
x.sort().map((v) => v * 2);
(x..sort()).map((v) => v * 2);
// ↓ 正解
@mono0926
Copy link
Author

mono0926 commented Feb 20, 2019

正解

final x = [3, 5, 1];

// [1, 3, 5] (mapの戻り値がうっかり破棄されてしまっている)
x..sort()..map((v) => v * 2);

// コンパイルエラー (..と.を直に繋げたので)
x..sort().map((v) => v * 2);

// コンパイルエラー (sortの戻り値はvoidなので)
x.sort().map((v) => v * 2);

// (2, 6, 10) (Iterable型だがさらに`.toList()`するとList型の[2, 6, 10]となる)
(x..sort()).map((v) => v * 2);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment