0

I'd like to create flexible logic that allows operating with complex wrapper objects via interfaces. But sometimes these objects can be just an instance of simple object that they are "wrapping". Something like this:

interface ICoords {
    var x:Float;
    var y:Float;
}

interface ICoordsAccess {
    var coords(default, null):ICoords;
}

class DumbAccess implements ICoordsAccess implements ICoords {
    // ICoordsAccess interface
    public var coords(default, null):ICoords;

    // ICoords interface
    public var x:Float;
    public var y:Float;

    public function new() {
        coords = this; // ...dumb...
    }
}

class SmartAccess implements ICoordsAccess implements ICoords {
    // ICoordsAccess interface
    public var coords(get, null):ICoords; // doesn't comply with interface signature

    function get_coords() return this; // CUTE!

    // ICoords interface
    public var x:Float;
    public var y:Float;

    public function new() {
        // empty constructor
    }
}

class WrapperAccess implements ICoordsAccess {  
    // ICoordsAccess interface
    public var coords(default, null):ICoords;

    public function new() {
        coords = new DumbAccess(); // or SmartAccess doesn't matter
    }
}

Code operating with instances of IObjectAccess and doesn't taking care of what it is. I can use concrete instance with constant values or wrapper that changes them on it's own logic.

But I'd like to neatly comply with haxe read-only access signature (default, null) by substituting getter function (probably inline?) that excludes storage of this reference in the real class variable. Signature (get, null) doesn't comply with demands of interface, but guaranteed that variable will be real, not just substitution for getter/setter functions.

Is my plan realizable? Or should I change my approach of the task?

Gama11
  • 31,714
  • 9
  • 78
  • 100
meps
  • 579
  • 2
  • 17

1 Answers1

0

I've decided to use coords(get, null) access in the core ICoordsAccess interface. And then I implement this signature in "SmartAccess" style:

inline function get_coords() return this;

I'm not really sure that this function became inline anyway.

Gama11
  • 31,714
  • 9
  • 78
  • 100
meps
  • 579
  • 2
  • 17