Struct "inheritance"
Vidar Wahlberg
canidae at exent.net
Sat Feb 4 04:55:55 PST 2012
On 2012-02-04 13:06, Simen Kjærås wrote:
> It seems that what you want is alias this:
Thank you both, that's exactly what I needed.
Leeching a bit more on the thread:
Going back to the method:
int somethingNifty(Point p) {
return p.x + p.y;
}
Let's say I have the following code:
for (x; 0 .. 10) {
for (y; 0 .. 10) {
Point p = {x, y};
somethingNifty(p);
}
}
[How] can you rewrite those two statements inside the loops to a single
line? For example (this doesn't work):
somethingNifty(Point(x, y));
And finally a question about operator overloading, here's the code for
"Coordinate" ("Point" is similar in structure):
import Point;
struct Coordinate {
Point _point;
int _z;
@property auto point() const {
return _point;
}
@property auto point(Point point) {
return _point = point;
}
@property auto z() const {
return _z;
}
@property auto z(int z) {
return _z = z;
}
bool opEquals(ref const Coordinate c) const {
return z == c.z && point == c.point;
}
}
Compilation fails with the following error:
Coordinate.d:18: Error: function Point.Point.opEquals (ref const
const(Point) p) const is not callable using argument types
(const(Point)) const
Coordinate.d:18: Error: c.point() is not an lvalue
Noteworthy the code compiles fine if i replace "point" and "c.point"
with "_point" and "c._point", but then I'm referencing _point directly
instead of through the property "point" (which may do something else
than just return _point in the future).
I've looked at
http://www.d-programming-language.org/operatoroverloading.html#equals
and that page is slightly confusing. It claims that:
If structs declare an opEquals member function, it should follow the
following form:
struct S {
int opEquals(ref const S s) { ... }
}
However, I can't even get the code to compile if I do that, the compiler
(gdc-4.6) says:
Error: function Coordinate.Coordinate.opEquals type signature should be
const bool(ref const(Coordinate)) not int(ref const const(Coordinate) c)
I hope it's somewhat clear what I'm trying to achieve.
More information about the Digitalmars-d-learn
mailing list