steve richey
steve richey

Reputation: 505

Overriding parent methods with contravariant arguments

Basically, I want to override a parent class with different arguments. For example:

class Hold<T> {
    public var value:T;
    public function new(value:T) {
        set(value);
    }
    public function set(value:T) {
        this.value = value;
    }
}

Then override that class, something like:

class HoldMore extends Hold<T> {
    public var value2:T;
    public function new(value:T, value2:T) {
        super(value);
        set(value, value2);
    }
    override public function set(value:T, value2:T) {
        this.value = value;
        this.value2 = value2;
    }
}

Obviously this will return an error, Field set overloads parent class with different or incomplete type. Is there a way around this? I tried using a public dynamic function, and then setting set in the new() function, but that gave a very similar error. Any thoughts?

Upvotes: 5

Views: 663

Answers (4)

dagnelies
dagnelies

Reputation: 5329

This is just a complement to @stroncium's answer, which is totally correct.

Here is an example how it could look like:

class Hold<T> {
    public var value:T;
    public function new(value:T) {
        set(value);
    }
    public function set(value:T) {
        this.value = value;
    }
}

class HoldMore<T> extends Hold<T> {
    public var value2:T;
    public function new(value:T, value2:T) {
        super(value);
        setBoth(value, value2);
    }
    // you cannot override "set" with a different signature
    public function setBoth(value:T, value2:T) {
        this.value = value;
        this.value2 = value2;
    }
}

alternatively, you could use an array as parameter or a dynamic object holding multiple values in order to "set" them using the same method, but you loose some of the compiler's type checking.

Upvotes: 4

stroncium
stroncium

Reputation: 1430

In the current state it totally won't work. There is not only 1 problem, but few of them:

  1. Type T is meaningless in context of this new class, you should either use some concrete type or template this class over T.
  2. You can not change the number of arguments of function when overriding it. However you can add another function(with a different name) to accept 2 arguments and do what you want (which is the way you would use in most languages, by the way).
  3. I don't really understand how you see a contravariance problem there. The actual problem is that haxe doesn't support function overload. (It actually does, the function signature is name + full type, but that's not what you would want to write nor support, and is mostly used for js/java externs.)

Upvotes: 1

npretto
npretto

Reputation: 1125

If you wrote the base class you could add an optional argument to it, this would be a workaround though, not directly what you want to do.

Upvotes: 1

Franco Ponticelli
Franco Ponticelli

Reputation: 4430

Unfortunately the language doesn't allow it.

Upvotes: 0

Related Questions