will do!
Only structures and classes -> structures use structural subtyping, you
cannot have structural subtyping between two classes for instance
(that's classic inheritance then).
Check http://haxe.org/manual/struct
Best,
Nicolas
typedef SPoint =
{
var x : Int;
var y : Int;
function add( p : Point ) : Void;
}
class CPoint
{
public var x : Int;
public var y : Int;
public function new(x,y)
{ this.x = x;
this.y = y;
}
public function add( p : Point ): Void {...}
}
var cpt: CPoint = new CPoint(0,3);
var spt: SPoint = { x : 0, y : 5, add : cpt.add }; // no error. good
spt.add = cpt.add; // forbidden since spt.add is not marked as dynamic . good
var cpt2: CPoint = spt;
//ERROR { y : Int, x : Int, add : p : Point -> Void } should be CPoint
// why ? cpt2 is a CPoint so modifying add field is forbidden. ( and spt.add is not marked dynamic)
// I can't try (will not be allowed) to modify the function reference. The structures are compatibles.
// finally at syntax level, no differences between
cpt.add({x:3, y:4});
spt.add({x:3, y:4});
typedef SDPoint =
{
var x : Int;
var y : Int;
dynamic function add( p : Point ) : Void; // == var add:Point->Void;
}
var sdpt: SDPoint;
sdpt = { x : 0, y : 5, add : cpt.add }; //no error . good.
spt = { x : 0, y : 5, add : cpt.add }; //no error . good
spt = cpt; // no error . good.
sdpt = spt; // error expected . good. Field add is method but should be dynamic method.sdpt = cpt; // error expected . good. Field add is method but should be dynamic method.
cpt = sdpt; //error is { y : Int, x : Int, add : p : Point -> Void } should be CPoint.
// the expected error should be 'Field add is dynamic but should be method.'
cpt = spt; //error is { y : Int, x : Int, add : p : Point -> Void } should be CPoint.
// ?? struct and access levels are compatibles. This expression should be allowed
I understand why actually we can't do some variances. It's really for goods reasons (for example you don't want member method to be replaced at runtime...) and you want also to statically control, at compile time some level of variance, visibility and Access control. I agree to offers that to the designer!
Also having the choice to switch between struct and classes in both way would be really great. My questions were about that.
>A struct is an abstract definition.
typedef SPoint =
{
var x : Int;
var y : Int;
function add( p : Point ) : Void;
}
interface ICPoint
{
var x:Int;
var y:Int;
function add( p : Point ): Void;
}
var spt: SPoint;
var icpt: ICPoint;
...
spt = icpt; // no error expected . good !
icpt = spt; // error { y : Int, x : Int, add : p : Point -> Void } should be ICPoint
// icpt, spt each are abstract like you said. but this variance is not allowed.