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?