Is it possible to ensure const correctness in Haxe? It’s pretty important in a project where data passing is done to multiple parties. I want to ensure other programmers won’t try to modify my data while it is being exposed for reading. They should be able to read it and modify it using a particular but different function.
A very simple example is here:
//Here the returned geometry should be const
public function getGeometry():Geometry {
return mGeometry;
}
//We might want to declare GeometryTransformation as const to insure
it will not be modified here and will be reusable for other cases.
public function transform(GeometryTransformation):Void{
//...
mObservers.sendMessage(GeometryEvent.TRANSFORMATION_OCCURED, this, mGeometry);
}
There are tons of cases where const correctness improves the readability of the code without having to refer to documentation. It also saves us from insidious and hard to find bugs.
What you can always do is define an immutable super type (interface, super class or anonymous type) of the data you want to pass around and then have all client be written against that.
There is no such thing as const though. The closest you get are read-only fields.