September 03, 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
September 03, 2015
On Thursday, 3 September 2015 at 18:42:37 UTC, H. S. Teoh wrote:
> 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.

what is this, javascript? a|0 :P

Seriously though, I never knew that. And I can't find where unary + is defined in D... I see the grammar line, but not explanation of what it actually does.

Perhaps it is meant to be self-evident!
September 03, 2015
On Thu, Sep 03, 2015 at 06:44:54PM +0000, Adam D. Ruppe via Digitalmars-d wrote:
> On Thursday, 3 September 2015 at 18:42:37 UTC, H. S. Teoh wrote:
> >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.
> 
> what is this, javascript? a|0 :P
> 
> Seriously though, I never knew that.

I didn't either, until I googled it just now. :-D


> And I can't find where unary + is defined in D... I see the grammar line, but not explanation of what it actually does.
> 
> Perhaps it is meant to be self-evident!

Hmm, apparently it does *not* do type promotion in D, at least according to this code:

	ubyte x = 1;
	pragma(msg, typeof(x));		// prints "ubyte"
	pragma(msg, typeof(+x));	// prints "ubyte"

So sounds like it's a real no-op in D.


T

-- 
Nobody is perfect.  I am Nobody. -- pepoluan, GKC forum
September 03, 2015
On Thursday, 3 September 2015 at 18:31:59 UTC, Meta wrote:
>
> 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.

I mean, if behaves exactly as I'd expect. +(-1) must equal -1, as -(-1) produces +1.

Also, recall that you can overload the unary + operator, so it may behave differently for some aggregate types.
September 03, 2015
On Thursday, 3 September 2015 at 18:37:53 UTC, Luís Marques wrote:
>
> I like to use the unary plus *in some cases* of tabular data, to reinforce the signess. Something like this:
>
> auto foo = [
>     +1234,
>     -5678,
>     -4242,
>     +9999
> ];

I don't do it often, but I've seen lots of people do it too in other languages. Most notably, I see a lot of the older people in my office will start excel equations with + instead of =, then excel will add in a =+. I think something like Lotus-1-2-3 did things that way and they never changed.
September 03, 2015
On Thursday, 3 September 2015 at 17:17:26 UTC, Steven Schveighoffer wrote:
> What about all other operations that may be typos from op= where op is also a unary operator? e.g. =-

We'd have to special-case '*':

a=*b;

September 03, 2015
On Thu, Sep 03, 2015 at 09:59:52PM +0000, Brian Schott via Digitalmars-d wrote:
> On Thursday, 3 September 2015 at 17:17:26 UTC, Steven Schveighoffer wrote:
> >What about all other operations that may be typos from op= where op is also a unary operator? e.g. =-
> 
> We'd have to special-case '*':
> 
> a=*b;

Not if we also look for whitespace after the '*'.

But then applying the same rule to + would cause us to miss cases like a=+b while catching a =+ b.


T

-- 
Latin's a dead language, as dead as can be; it killed off all the Romans, and now it's killing me! -- Schoolboy
September 04, 2015
On 9/3/15 5:59 PM, Brian Schott wrote:
> On Thursday, 3 September 2015 at 17:17:26 UTC, Steven Schveighoffer wrote:
>> What about all other operations that may be typos from op= where op is
>> also a unary operator? e.g. =-
>
> We'd have to special-case '*':
>
> a=*b;
>

You could say the same thing for =-:

a=-b;

seems reasonable for someone who doesn't like whitespace. I think Andrei's rule was the token sequence must have whitespace after the operator in order to be rejected. So the above would be fine.

-Steve
September 04, 2015
On Thursday, 3 September 2015 at 16:46:30 UTC, Andrei Alexandrescu wrote:
> I wonder if we should disallow during tokenization the sequence "=", "+", whitespace. Surely it's not a formatting anyone would aim for, but instead a misspelling of +=.
>
>
> Andrei


Please don't. That would feel like a completely arbitrary exception in the grammar.

September 04, 2015
On Thursday, 3 September 2015 at 18:31:59 UTC, Meta 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.

I didn't realize that a unary + operator existed. I would have assumed that +a as an expression would be illegal. It doesn't even mean anything.

- Jonathan M Davis