mutable constant?

Jonathan M Davis jmdavisProg at gmx.com
Tue Jun 25 15:51:30 PDT 2013


On Wednesday, June 26, 2013 00:07:38 Namespace wrote:
> I want to ask if this code should compile or if it's a bug,
> because I circumvent the const system:
> 
> ----
> import std.stdio;
> 
> struct Point {
> int x, y;
> }
> 
> Point*[] points;
> 
> struct TplPoint(T) {
> public:
> Point _point;
> 
> T x, y;
> 
> const uint id;
> 
> this(T x, T y) {
> this.x = x;
> this.y = y;
> 
> points ~= &this._point;
> 
> id = points.length - 1;
> }
> 
> @property
> inout(Point)* ptr() inout {
> points[this.id].x = cast(int) this.x;
> points[this.id].y = cast(int) this.y;
> 
> return cast(inout Point*) points[this.id];
> }
> }
> 
> void main() {
> const TplPoint!float my = TplPoint!float(42, 23);
> writeln(my._point, "::", &my._point);
> writeln(*my.ptr, "::", my.ptr);
> }
> ----
> 
> Or is the fact that it compiles ok and it's "only" unsafe?

I could certainly be missing something here, but I don't understand what
about the code you're even concerned about. Where in here would you be
breaking the type system? I don't see any place in here where you're
mutating a const variable or anything like that. The worst thing I see
about the code is that it won't compile on 64-bit machines thanks to

id = points.length - 1;

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list