jfp
jfp

Reputation: 421

Error: Invalid override in ==

The following error:

ERROR: Invalid override. The type of Bar.== ((Bar) → bool) is not a subtype of 
Foo.== ((Foo) → bool). 

Occurs in line 10 of the following code (v1.15.0):

1  class Foo {
2    int foo;

3    Foo(this.foo);

4    bool operator ==(Foo a) => foo == a.foo;
5    int get hashCode => foo * 17;
6  }

7  class Bar extends Foo {
8    int bar;

8    Bar(int foo, this.bar) : super(foo);

10   bool operator ==(Bar a) => (bar == a.bar) && (foo == a.foo);
11   int get hashCode => bar * (foo * 17);
12 }

I have assumed that Bar is a subtype of Foo. What is the problem with this code?

Upvotes: 0

Views: 1641

Answers (2)

Yugene
Yugene

Reputation: 3232

The problem is that you mentioned Class name in operator ==

1  class Foo {
2    int foo;

3    Foo(this.foo);

4    bool operator ==(Foo a) => foo == a.foo;
5    int get hashCode => foo * 17;
6  }

7  class Bar extends Foo {
8    int bar;

8    Bar(int foo, this.bar) : super(foo);

10   bool operator ==(Bar a) => (bar == a.bar) && (foo == a.foo);
11   int get hashCode => bar * (foo * 17);
12 }

bool operator ==(Foo a) => foo == a.foo;

bool operator ==(Bar a) => (bar == a.bar) && (foo == a.foo);

These two lines cause problems.

You must update these lines like this.

bool operator ==(a) => foo == a.foo;

bool operator ==(a) => (bar == a.bar) && (foo == a.foo);

Upvotes: 1

jfp
jfp

Reputation: 421

This may not be a bug. I just realized that the == operator should probably have a type of Object to work effectively. A better error message would be helpful.

Upvotes: 1

Related Questions