I have two custom classes called `OrthogonalPoint` and `PolarPoint`.

```
public class OrthogonalPoint extends AbstractPoint<OrthogonalPoint> {
    private double x;
    private double y;

    // constructor, getters, ...
}

```

```
public class PolarPoint extends AbstractPoint<PolarPoint> {
    private double rho;
    private double theta;

    // constructor, getters, ...
}

```

Both extend an abstract class `AbstractPoint`.

```
public abstract class AbstractPoint<PointType extends AbstractPoint<PointType>> {

    public abstract double getX();

    public abstract double getY();

    public final double distance(PointType other) {
        // calculate distance between current point and other point
    }
}

```

Next, I have a class `Route`.

```
public class Route<T extends AbstractPoint<T>> {

    private final List<T> points = new ArrayList<>();

    public void appendPoint(T point) {
        this.points.add(point);
    }

    public double routeDistance() {
        // calculate total distance
    }
}

```

A route is an ordered number of points whose length is defined as the sum of the distances of the points in the sequence. So, if a route consists of three points (p1, p2, and p3) the distance of the route is `p1.distance(p2) + p2.distance(p3)`.
In the `routeDistance` function, I want to calculate this distance. I have tried something like

```
public double routeDistance() {
    return this.points.stream().reduce(0d, (point, point2) -> point.distance(point2));
}

```

But the body part of the lambda function is underlined red:

```
Bad return type in lambda expression: double cannot be converted to T

```

What would be the correct way to calculate the distance? I would like to use the reduce, but any solution is appreciated.