In Dart Strong-Mode, can I leave types from function definitions?

For example, I would just like to write:

class Dog {
  final String name;

  Dog(this.name);

  bark() => 'Woof woof said $name';
}

But the definition of the type is #Dog.barkbe () => String.

This was not possible in Dart 1.x, but I hope that type inference can save the day and avoid the need to introduce trivial functions where the return type is output (same as for closing today?).

+4
source share
1 answer

There are currently no plans in the language team to infer the types of returned members based on their bodies. There are cases where this would be good, but there are other cases (for example, recursive methods) where this does not work.

With the conclusion, we must balance several opposing forces:

  • , , , .

  • , , API, . , API.

  • , , .

, , . , . , API , , .

, , :

  • Dart . , , map() , .

  • , . Beagle.bark() :

    class Dog {
      String bark() => "Bark!";
    }
    
    class Beagle extends Dog {
      final String name;
    
      Dog(this.name);
    
      bark() => 'Woof woof said $name';
    }
    
+5

Source: https://habr.com/ru/post/1668675/


All Articles