'int' is enough for 'length' to migrate code from x86 to x64

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Fri Nov 21 09:08:54 PST 2014


On Fri, Nov 21, 2014 at 08:31:13AM -0800, Andrei Alexandrescu via Digitalmars-d wrote:
> On 11/21/14 6:03 AM, ketmar via Digitalmars-d wrote:
> >On Thu, 20 Nov 2014 13:28:37 -0800
> >Walter Bright via Digitalmars-d <digitalmars-d at puremagic.com> wrote:
> >
> >>On 11/20/2014 7:52 AM, H. S. Teoh via Digitalmars-d wrote:
> >>>What *could* be improved, is the prevention of obvious mistakes in
> >>>*mixing* signed and unsigned types. Right now, D allows code like
> >>>the following with no warning:
> >>>
> >>>	uint x;
> >>>	int y;
> >>>	auto z = x - y;
> >>>
> >>>BTW, this one is the same in essence as an actual bug that I fixed
> >>>in druntime earlier this year, so downplaying it as a mistake
> >>>people make 'cos they confound computer math with math math is
> >>>fallacious.
> >>
> >>What about:
> >>
> >>      uint x;
> >>      auto z = x - 1;
> >>
> >>?
> >>
> >here z must be `long`. and for `ulong` compiler must emit error.

What if x==uint.max?


> Would you agree that that would break a substantial amount of correct
> D code? -- Andrei

Yeah I don't think it's a good idea for subtraction to yield a different
type from its operands. Non-closure of operators (i.e., results are of a
different type than operands) leads to a lot of frustration because you
keep ending up with the wrong type, and inevitably people will just
throw in random casts everywhere just to make things work.


T

-- 
We are in class, we are supposed to be learning, we have a teacher... Is it too much that I expect him to teach me??? -- RL


More information about the Digitalmars-d mailing list