regarding what seems (to me) unnecessary casts on integer expressions

someone someone at somewhere.com
Sat Jun 5 00:24:01 UTC 2021


Consider the following code to keep track of rectangle positions 
on the screen; eg: (1,2)(3,2) ie: (left,top)(right,bottom), 
providing get/set properties for one-based positions but only get 
properties for zero-based positions:

```d
public struct gudtPosition {

    final public void reset() { pintLeft1 = pintTop1 = pintRight1 
= pintBottom1 = 0; }

    private ushort pintLeft1 = 0;
    private ushort pintTop1 = 0;
    private ushort pintRight1 = 0;
    private ushort pintBottom1 = 0;

    final public const ushort left1() { return this.pintLeft1; }
    final public const ushort top1() { return this.pintTop1; }
    final public const ushort right1() { return this.pintRight1; }
    final public const ushort bottom1() { return this.pintBottom1; 
}

    final public ushort left0() { return this.pintLeft1 = 0 ? 0 : 
cast(ushort)(this.pintLeft1 - 1); }
    final public ushort top0() { return this.pintTop1 = 0 ? 0 : 
cast(ushort)(this.pintTop1 - 1); }
    final public ushort right0() { return this.pintRight1 = 0 ? 0 
: cast(ushort)(this.pintRight1 - 1); }
    final public ushort bottom0() { return this.pintBottom1 = 0 ? 
0 : cast(ushort)(this.pintBottom1 - 1); }

    final public void left1(const ushort lintPosition) { 
this.pintLeft1 = lintPosition; }
    final public void top1(const ushort lintPosition) { 
this.pintTop1 = lintPosition; }
    final public void right1(const ushort lintPosition) { 
this.pintRight1 = lintPosition; }
    final public void bottom1(const ushort lintPosition) { 
this.pintBottom1 = lintPosition; }

    final public string caption0() { return /// eg: from (24,24) 
to (48,48)
       r"from ("c ~
       std.format.format(r"%d"c, this.left0) ~
       r","c ~
       std.format.format(r"%d"c, this.top0) ~
       r") to ("c ~
       std.format.format(r"%d"c, this.right0) ~
       r","c ~
       std.format.format(r"%d"c, this.bottom0) ~
       r")"c;
    }

    final public string caption1() { return /// eg: from (24,24) 
to (48,48)
       r"from ("c ~
       std.format.format(r"%d"c, this.pintLeft1) ~
       r","c ~
       std.format.format(r"%d"c, this.pintTop1) ~
       r") to ("c ~
       std.format.format(r"%d"c, this.pintRight1) ~
       r","c ~
       std.format.format(r"%d"c, this.pintBottom1) ~
       r")"c;
    }

}
```

The code should be, I guess, self-explanatory to read. I do have 
some things to learn here on the D-specifics:

- is there a way to unambiguously specify short integers values 
like there is for long ones (eg: 1L means 1 long integer like: 
long pintSomething = 1L;) ?

- that follows to what seems to be unnecessary castings in my 
code snippet; if I write: ushort a = 1; ushort b = 2; ushort c; 
why can't I write c = a + b; ? ... DMD refuses the last one 
unless I explicitly write: c = cast(ushort) a + b; ... if all the 
variables I defined are of the same type ... why should I do have 
to cast() them afterwards ?


More information about the Digitalmars-d-learn mailing list