Jump to page: 1 26  
Page
Thread overview
yank unary '+'?
Dec 06, 2009
bearophile
Dec 06, 2009
BCS
Dec 06, 2009
Don
Dec 07, 2009
Rainer Deyke
Dec 07, 2009
Don
Dec 07, 2009
Bill Baxter
Dec 08, 2009
Lukas Pinkowski
Dec 08, 2009
Bill Baxter
Dec 07, 2009
retard
Dec 07, 2009
Nick Sabalausky
Dec 08, 2009
Mike Parker
Dec 08, 2009
retard
Dec 08, 2009
Mike Parker
Dec 08, 2009
klickverbot
Dec 08, 2009
KennyTM~
Dec 06, 2009
KennyTM~
Dec 06, 2009
Don
Dec 06, 2009
KennyTM~
Dec 06, 2009
Don
Dec 06, 2009
Walter Bright
Dec 06, 2009
Walter Bright
Dec 06, 2009
Don
Dec 06, 2009
Walter Bright
Dec 06, 2009
KennyTM~
Dec 07, 2009
Don
Dec 07, 2009
Walter Bright
Dec 07, 2009
KennyTM~
Dec 06, 2009
Walter Bright
Dec 07, 2009
Don
Dec 07, 2009
bearophile
Dec 07, 2009
Don
Dec 07, 2009
Brad Roberts
Dec 07, 2009
Johan Granberg
Dec 07, 2009
Denis Koroskin
Dec 07, 2009
Johan Granberg
Dec 08, 2009
Michel Fortin
Dec 08, 2009
Michel Fortin
Dec 08, 2009
Denis Koroskin
Dec 07, 2009
Justin Johansson
Dec 07, 2009
Simen kjaeraas
Dec 08, 2009
Walter Bright
Dec 08, 2009
Brad Roberts
Dec 08, 2009
Nick Sabalausky
December 06, 2009
Is there any good use of unary +? As an aside, Perl programs do use it occasionally for syntactic disambiguation :o).

Andrei
December 06, 2009
Andrei Alexandrescu:

> Is there any good use of unary +?

In an array of directions I have used -something to mean left and +something to mean right, to keep their symmetry. This is an usage, but not a good enough one.

Bye,
bearophile
December 06, 2009
Andrei Alexandrescu wrote:
> Is there any good use of unary +? As an aside, Perl programs do use it occasionally for syntactic disambiguation :o).
> 
> Andrei

In theory, it could be used in floating point for emphasizing that 0.0 is +0.0 and not -0.0. But that may not be what you mean.
Outside of literals, the number of cases I've seen is
+(+(+(+(+0))))

December 06, 2009
Andrei Alexandrescu wrote:

> Is there any good use of unary +?

I think it's just good symmetry. It makes a programming language just a little bit more elegant. Plus, you keep the 0.01% of programmers that use it happy.

What will removing it gain you?

-- 
Michiel Helvensteijn

December 06, 2009
On Dec 7, 09 00:23, Andrei Alexandrescu wrote:
> Is there any good use of unary +? As an aside, Perl programs do use it
> occasionally for syntactic disambiguation :o).
>
> Andrei

Yes, when you want to port the Boost Spirit parser :o) (OK that's an abuse.)

Well the unary + can help to emphasize "it's a positive number", and 1.0e+10 is already a form of "unary +" (not the operator).

Removing the unary + doesn't lose much, but it doesn't gain much either, and with it already present in all other languages, I don't see a good reason to change it.
December 06, 2009
Andrei Alexandrescu wrote:
> Is there any good use of unary +? As an aside, Perl programs do use it occasionally for syntactic disambiguation :o).


http://stackoverflow.com/questions/727516/what-does-the-unary-plus-operator-do
December 06, 2009
Andrei Alexandrescu wrote:
> Is there any good use of unary +? As an aside, Perl programs do use it occasionally for syntactic disambiguation :o).

An internet search reveals:

1. symmetry

2. compatibility with C and many other languages that use it

3. used with operator overloading to convert a user defined type to its preferred arithmetic representation (a cast can't know what the 'preferred' type is)

4. to create DSL languages, like Spirit, as Kenny points out

5. to coerce default integral promotion rules (again, cast(int) won't always produce the same result)

6. to visually emphasize that a literal is positive

I say leave it in.
December 06, 2009
KennyTM~ wrote:
> On Dec 7, 09 00:23, Andrei Alexandrescu wrote:
>> Is there any good use of unary +? As an aside, Perl programs do use it
>> occasionally for syntactic disambiguation :o).
>>
>> Andrei
> 
> Yes, when you want to port the Boost Spirit parser :o) (OK that's an abuse.)
> 
> Well the unary + can help to emphasize "it's a positive number", and 1.0e+10 is already a form of "unary +" (not the operator).
> 
> Removing the unary + doesn't lose much, but it doesn't gain much either, and with it already present in all other languages, I don't see a good reason to change it.

I think + should be added to the syntax for numeric literals, and in all other cases unary + should be dropped.
Ie,
x = +0.78; should remain legal.
But
y = +x;  should not.
And likewise,
x = +(+0.78); should be illegal.

Overloading + is odd, too. Currently:
+x;
creates a "has no effect" error if x is a built-in type. But if x has an overloaded unary +, it might have side-effects. So it useful ONLY for operator abuse!


December 06, 2009
On Dec 7, 09 04:30, Don wrote:
> KennyTM~ wrote:
>> On Dec 7, 09 00:23, Andrei Alexandrescu wrote:
>>> Is there any good use of unary +? As an aside, Perl programs do use it
>>> occasionally for syntactic disambiguation :o).
>>>
>>> Andrei
>>
>> Yes, when you want to port the Boost Spirit parser :o) (OK that's an
>> abuse.)
>>
>> Well the unary + can help to emphasize "it's a positive number", and
>> 1.0e+10 is already a form of "unary +" (not the operator).
>>
>> Removing the unary + doesn't lose much, but it doesn't gain much
>> either, and with it already present in all other languages, I don't
>> see a good reason to change it.
>
> I think + should be added to the syntax for numeric literals, and in all
> other cases unary + should be dropped.
> Ie,
> x = +0.78; should remain legal.
> But
> y = +x; should not.
> And likewise,
> x = +(+0.78); should be illegal.
>
> Overloading + is odd, too. Currently:
> +x;
> creates a "has no effect" error if x is a built-in type. But if x has an
> overloaded unary +, it might have side-effects. So it useful ONLY for
> operator abuse!
>
>

import std.math;

auto theta1 = +PI/6;
auto theta2 = -PI/8;
December 06, 2009
Walter Bright wrote:
> Andrei Alexandrescu wrote:
>> Is there any good use of unary +? As an aside, Perl programs do use it occasionally for syntactic disambiguation :o).
> 
> An internet search reveals:
> 
> 1. symmetry

I think it makes sense for literals. Not for anything else though.
Since + is a no-op, it just causes confusion.

> 2. compatibility with C and many other languages that use it

That matters only if those other languages actually have a use for it.

> 3. used with operator overloading to convert a user defined type to its preferred arithmetic representation (a cast can't know what the 'preferred' type is)
> 5. to coerce default integral promotion rules (again, cast(int) won't
> always produce the same result)

This one is interesting, and might be the strongest argument, but I don't understand it. An example would be interesting.

> 4. to create DSL languages, like Spirit, as Kenny points out

If we are to have a feature specifically for DSL languages, it's not hard to come up with something more useful...
(From memory, Spirit uses it as the nearest available approximation to postfix +).

> 6. to visually emphasize that a literal is positive
Yes, I think this is the strongest. But this doesn't need unary + in general, I don't think. Just for numeric literals.
« First   ‹ Prev
1 2 3 4 5 6