Timur Fayzrakhmanov
Timur Fayzrakhmanov

Reputation: 19587

Dart. No warnings and errors when class doesn't implement an interface

I can't understand this "weird" behavior of dart. Look at the sample above:

abstract class Swimmer {
  int numOfSwim;
  Swim();
}

class Human implements Swimmer {

}

When I run this code there is no warning and errors. I start dartium with DART_FLAGS='--enable_type_checks --enable_asserts'. What the hell? How the class can implement an interface if it actually doesn't.. Looks like Dart is a little bit loose.

May be Dart has some mode to be more strict.

Upvotes: 0

Views: 343

Answers (1)

Pixel Elephant
Pixel Elephant

Reputation: 21383

The Dart spec states (p38):

It is a static warning if a concrete class does not have an implementation for a method in any of its superinterfaces unless it declares its own noSuchMethod method (10.10).

Static warnings are useful for developers to catch mistakes but do not interfere with the execution of a Dart program. Errors on the other hand do have an effect on program execution. You can read more about the difference between warning and errors on page 10 of the Dart spec.

Since this is the case, running your code in Dartium will work just fine, since a missing method implementation is just a warning, not an error. If you want to catch these warnings, you can use the Dart Analyzer which will check for warnings and errors. IDEs such as Dart Editor and IntelliJ integrate the analyzer, so you don't have to run it manually.

For instance, in Dart Editor your code will display the following warning:

Missing concrete implementation of setter 'Swimmer.numOfSwim', 'Swimmer.Swim' and getter 'Swimmer.numOfSwim'

Upvotes: 2

Related Questions