Miras Kairanov
Miras Kairanov

Reputation: 1

Dart strong mode: on casting implementation to abstract class implementation's are still visible and compiler is not throwing errors

My abstract class:

abstract class Animal {
  void saySomething() {
    debugPrint("I'm an animal");
  }
}

My implementation class:

class Cow implements Animal {
  int _mooCount = 0;
  Cow();
  Cow.withCount(this._mooCount);

  Cow action({
    String sound,
    int setCount,
    int removeCount,
    bool flushCount
  }) {
    this._mooCount++;
    if (setCount != null) {
      this._mooCount = setCount;
    }
    if (sound != null) {
      debugPrint(sound + ",count= " + _mooCount.toString());
    } else {
      debugPrint("Mooo");
    }
    return this;
  }

  void saySomething() {
    debugPrint("Auuuuuuuuuuuuuuu");
  }
}

How do i use it:

Animal myCow = new Cow();
myCow =  myCow.action(setCount: 0).action(sound: "Moooooooo");

Intellij IDEA says:

The method 'action' isn't defined for the class 'Animal'

But everything compiles well in strong mode.

analysis_options.yaml :

analyzer:
  strong-mode: true

flutter output:

I/flutter ( 6325): Mooo
I/flutter ( 6325): Moooooooo,count= 1
I/flutter ( 6325): Auuuuuuuuuuuuuuu

Upvotes: 0

Views: 980

Answers (2)

Miras Kairanov
Miras Kairanov

Reputation: 1

As Vyacheslav Egorov said, strong mode has no influence on Dart VM at the moment.

Upvotes: 0

Rainer Wittmann
Rainer Wittmann

Reputation: 7978

Please correct me, if I'm wrong. You initialise my Cow as an Animal and before the code is compiled the IntelliJ analyser doesn't know that your Animal is actually a Cow. Thats the reason why IntelliJ tells you that Animal has no action method, which is correct. Once compiled action can be called on myCow, because now it is clear that it's actually a cow.
How about implementing the action method in Animal as well?

Upvotes: 1

Related Questions