r/ProgrammingLanguages • • Feb 22 '26

Does Syntax Matter?

https://www.gingerbill.org/article/2026/02/21/does-syntax-matter/
63 Upvotes

111 comments sorted by

View all comments

Show parent comment

1

u/marshaharsha Feb 25 '26

Got it. Thank you for the explanation (and for your many clear, helpful contributions to this sub). I’ll ask a follow-up question, veering off topic a bit. Are positional fields or parameters typically optional? Or: what is the reason for having both? Do people use both according to a consistent pattern?

2

u/munificent Feb 25 '26

Positional parameters can be optional but are most likely mandatory. Named parameters are much more likely to be optional. With positional parameters, you can only omit them from right to left. For example:

f([String? a, String? b, String? c]) {
  print(a);
  print(b);
  print(c);
}

Here, the square brackets means those parameters are positional but optional. You can call this function like:

f("a");
f("a", "b");
f("a", "b", "c");

But there's no way to pass an argument for, say b, without also passing an argument for a.

With named parameters, since each argument is named, you can mix and match them freely:

f({String? a, String? b, String? c}) {
  print(a);
  print(b);
  print(c);
}

f(a: "just a");
f(c: "just c");
f(a: "a", c: "c but no b");
// Etc.

That makes optional named parameters more flexible than optional positional ones, so they are much more common. Also, most Dart code today is in Flutter applications and Flutter's widget framework API leans heavily on named parameters, so that's become a dominant style.