0
votes

1. There is confliction on Dart Language tour

In Functions section, it says

The => expr syntax is a shorthand for { return expr; }.

Note: Only an expression—not a statement—can appear between the arrow (=>) and the semicolon (;). For example, you can’t put an if statement there, but you can use a conditional expression.

But in the Anonymous functions section, it says

If the function contains only one statement, you can shorten it using arrow notation

Does it mean I can use statement which is not an expression (such as if statement) in anonymous functions?

var fun = () => return 3; // However, this doesn't work.
var gun = () {
  return 3;               // this works.
}

Or am I confusing concept of expression and statement? I thought

  • expression : can be evaluated to a value ( 2 + 3 , print('') also falls into an expression )
  • statement : code that can be executed. all expressions can be statement. if statement and return statement are examples of statement which is not expression.

2. Is this expression or statement

void foo() => true; // this works.
void goo() {
  return true;      // this doesn't work.
}
void hoo() {
  true;             // this works.
}

If true is understood as expression, then it will mean return true and I believe it should not work because foo's return type is void.

Then does it mean true in foo is understood as a statement? But this conclusion contradicts with dart language tour. (They are top-level named functions). Also, this means we can use statement with arrow syntax.


I use VSCode and Dart from flutter: 1.22.5. I tell code that works from code that doesn't work based on VSCode error message.

Because this is my first question, I apologize for my short English and ill-formed question.

2
I think that the "If the function contains only one statement, you can shorten it using arrow notation" is using "statement" in a casual and not technical sense. => requires an expression on the right-hand-side. It is true that a function with a single statement of return 3; can be shortened; the shortened form is => 3. in practice it's rare (but not impossible) to have a function with one statement that can't be shortened. - jamesdlin

2 Answers

0
votes

I'm guessing that the author of that section under Anonymous functions was a bit confused. File an issue against it, and get it corrected!

Yeah, even in their example they use a print() function, which they might be confusing as a print "statement", which it clearly is not.

0
votes

It must be an expression. The text is misleading.

For the second part, the error you see with

void foo() {
  return 0;
}

and not with

void bar() => 0;

is a special case for => in functions returning void. Normally, you can't return a value from a function with return type void, so no return exp;, only return;. (There are exceptions if exp has type void, null or dynamic, but yours doesn't).

Because people like the short-hand notation of void foo() => anything; so much, you are allowed to do that no matter what the type of anything is. That's why there is a distinction between void foo() { return 0; } and void foo() => 0;. They still mean the same thing, but the type-based error of the former is deliberately suppressed in the latter.