[Vala] Returning subclass objects from superclass, or type synonyms
- From: Maurizio Ferreira <ferreiramaurizio gmail com>
- To: vala-list gnome org
- Subject: [Vala] Returning subclass objects from superclass, or type synonyms
- Date: Tue, 28 Apr 2020 23:20:47 +0200
I need to define a function in a superclass that should return the same
type of object on which it is called
example : (constructors, functions and properties removed, for clarity)
class Tuple {
public Tuple add (Tuple another) {
// ...
}
}
class Point : Tuple {
// function 'add' inherited
}
The class Point is 99% equal to Tuple, with only a couple of added methods
now the test:
Tuple t = new Tuple (...);
Tuple t_other = new Tuple(...);
Tuple t_result = t.add(t_other); // ok, no problem
Point p = new Point (...);
Point p_other = new Point (...);
Point p_result = p.add(p_other); // compiler error
The compiler complains that the function return type is not assignable to a
variable of type Point (since it is a Tuple)
surely I can write
Tuple p_result = p.add(p_other); // ok
But this make the program less readable.
I know that this behaviour is typical of several oo languages,
but in Go (and Pascal and Delphi ...) it is possible to define type
synonyms, like
type Point = Tuple;
So that Point and Tuple are actually identical types.
Is it possible to have them also in Vala, or is it there a way to bypass
the problem ?
Best regards.
Maurizio.
[Date Prev][
Date Next] [Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]