Interesting user mistake

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Thu Sep 3 11:38:51 PDT 2015


On Thu, Sep 03, 2015 at 06:31:57PM +0000, Meta via Digitalmars-d wrote:
> On Thursday, 3 September 2015 at 17:12:31 UTC, H. S. Teoh wrote:
> >Is there a way for the lexer to check for the specific character
> >sequence '=', '+', whitespace and not others (e.g. '=', whitespace,
> >'+')?  IOW, "a =+ b" will be prohibited, but "a = + b" will be
> >allowed. If so, I agree with this.
> >
> >On that note, though, the unary + operator is totally useless in D...
> >maybe we should get rid of that instead?  (Then "=+" will
> >automatically be an error.)
> >
> >
> >T
> 
> Worse than useless; it doesn't even behave as you would expect.
> 
> import std.stdio;
> 
> void main()
> {
> 	auto a = -1;
> 	writeln(+a); //Prints -1
>         writeln(-a); //Prints  1
> }
> 
> At least unary - does something.

Sure it behaves as expected: it's the opposite of unary -, which negates
the sign, so unary + does not negate the sign. I.e., it's a no-op.
Therefore, it's useless. :-)

Now, in C, unary + actually does something -- it causes promotion of
narrow ints to int. I'm not sure if it does that in D as well.  But it's
one of those obscure things that people will probably only discover when
it shows up as a bug in their code. I'm in favor of killing it outright.


T

-- 
MSDOS = MicroSoft's Denial Of Service


More information about the Digitalmars-d mailing list