Home > OS >  Has the Dart lambda syntax for method an effect at runtime?
Has the Dart lambda syntax for method an effect at runtime?

Time:11-06

In JavaScript there is a difference between m1 and m2:

class A {
  m1() { return 123; }
  m2 = () => 123;
}

Here, m1 is stored in the prototype (it exists in the object that represents the class) while a copy of m2 is stored in each instance as a property. So the first syntax is better where it is adapted.

I would like to know if there is a similar difference in Dart for this kind of code:

class A {
  int m1() { return 123; }
  int m2() => 123; 
}

At runtime, are m1 and m2 completely equivalent?

CodePudding user response:

In Dart, there's no difference.

The docs explain it:

The => expr syntax is a shorthand for { return expr; }. The => notation is sometimes referred to as arrow syntax.

The JavaScript difference is due to historical reasons and no sane language would have the same distinction between the two notations.

  • Related