Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

void Function(int) isn't a valid override of void Function(dynamic)

Tags:

dart

class Parent<T> {
  void method(T t) {}
}

class Child extends Parent {
  @override
  void method(int i) {} // error: mentioned_below

  void takesDynamic(dynamic d) {
    takesType(d); // no error
  }

  void takesType(int i) {
    takesDynamic(i); // no error
  }
}

Error:

void Function(int) isn't a valid override of void Function(dynamic)

When I can easily pass int to dynamic and vice-versa in a method parameter, why do I see the error when I override method.


PS:

I am not looking for a solution which is to use extends Parent<int> and get it working, I want to know the reason why things are treated differently when I am overriding a method vs calling regular methods.

like image 772
iDecode Avatar asked Sep 16 '25 05:09

iDecode


1 Answers

void Function(int x) normally isn't a valid override of void Function(dynamic x) because the int version is not substitutable for the dynamic version.

What are the allowed inputs to void Function(dynamic) (the type of Parent<dynamic>.method)? Anything.

What are the allowed inputs to void Function(int) (the type of Child.method)? Only ints.

Such an override therefore could violate the contract of Parent<dynamic>'s interface. (For example, what if you had an instance of Child and passed it to something that expected Parent<dynamic>, which then invoked method('not an int') on it?)

(Note that this is not specific to method overrides. In general, a function that takes a narrower type cannot be used where a function that takes a wider type is expected, even if the narrower type derives from the wider type.)

Dart does allow you to use the covariant keyword to suppress the static type error and explicitly allow the override, but be aware that doing so isn't necessarily type-safe, and you would be responsible for ensuring that you don't get type errors at runtime.

Further reading: Covariance and contravariance (computer science) from Wikipedia

like image 151
jamesdlin Avatar answered Sep 17 '25 19:09

jamesdlin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!