Structs to Java?

IMHO, this would be a bad design. It should be possible that a struct can derive from abstract classes. at least, if it has no variables. This would allow s.th. like this:


abstract class Vector2 {

    abstract double getX();
    abstract double getY();

    abstract void setX(double x);
    abstract void setY(double y);

    abstract Vector2 new();

    Vector2 add(Vector3 left) {
        final  Vector2 sum = new();
        sum.setX(this.getX() + left.getX());
        sum.setY(this.getY() + left.getY());
        return sum;
    }
    
    <T extends Vector2> T add(Vector3 left, T sum) {
        sum.setX(this.getX() + left.getX());
        sum.setY(this.getY() + left.getY());
        return sum;
    }    

    ...

	// Having a primitive version..

    static class Double extends Vector2 {
	double x;
	double y;

	 Double new() {
	    return new Double();
	 }

	 double getX( { return this.x; }
	 double getY( { return this.y; }

	 void setX(double x) { this.x = x; }
	 void setY(double y) { this.y = y; }
}
    
    // just another one
    
    static class Float extends Vector2 {
	 float x;
	 float y;

	Float new() {
	     return new Float();
	 }

	 double getX( { return this.x; }
	 double getY( { return this.y; }

	 void setX(double x) { this.x = (float)x; }
	 void setY(double y) { this.y = (float)y; }
    }    
    
    
    // .. and a DoubleBuffer version

    static class DoubleBuffer extends Vector2 {
	 DoubleBuffer buffer;

	 Double() {
	    this(newDoubleBuffer());
	 }

	 Double(DoubleBuffer buffer) {
	    this.buffer = buffer;
	 }

	 Double new() {
	    return new Double();
	 }

	 double getX( { return this.buffer.get(0); }
	 double getY( { return this.buffer.get(1); }

	 void setX(double x) { this.buffer.put(x); }
	 void setY(double y) { this.buffer.put(y); }
    }

    // whereby the last one hopefully can be replaced by this one
    

    @Struct
    static class DoubleStruct extends Vector2 {
	@whatever annotations go here
	 float x;
	 @and here
	 float y;

	 Double new() {
	     return new Double();
	 }

	 double getX( { return this.x; }
	 double getY( { return this.y; }

	 void setX(double x) { this.x = x; }
	 void setY(double y) { this.y = y; }
    }
}

True. The single inheritance limit would be mostly wasted if it was used only to tag something as a Struct. An annotation makes more sense, or an interface even.

Except that the basic implementation of a mapped object must be the same for all objects. In fact the pattern is the same for the new enum feature in Java.

Cas :slight_smile: