Struct "inheritance"

bearophile bearophileHUGS at lycos.com
Sat Feb 4 05:03:11 PST 2012


Vidar Wahlberg:

> 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));

This works:

struct Point {
    int x, y;
}

int somethingNifty(Point p) {
    return p.x + p.y;
}

void main( ) {
    foreach (x; 0 .. 10)
        foreach (y; 0 .. 10)
            somethingNifty(Point(x, y));
}

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list