Grammar for plus and minus unary ops

(Originally posted to python-dev, discussion moved here per request by GvR; also fixed pseudo-code to not use a keyword as local var) Begin forwarded message:

A complete test suite would include such a case ;-).
-1 1) This would be a petty, gratuitous restriction that would only complicate the language and make it harder to learn for no real gain. 2) It could break code. + ob maps to type(ob).__pos__(ob), which could do anything, and no necessary just return ob as you are assuming. 3) Consider eval('+' + somecode). Suppose somecode happens to start with '+'. Currently the redundancy is harmless if not meaningful. In summary, I think the following applies here: "Special cases aren't special enough to break the rules." Terry Jan Reedy

If you want to make sure that you can't use ++ or --, then target that directly: add ++ and -- as tokens to the language and make them always illegal. While that might be a bit of a kludge, I think it's far better than adding a complicated rule that you can't put two + or two - in a row. And if you're worried about eval('+' + somecode), you've got three choices: (1) leave out the '+' because it has no effect; (2) write eval('+ ' + somecode) and (3) are you sure you really want to use eval? --- Bruce On Fri, Mar 27, 2009 at 4:49 PM, Terry Reedy <tjreedy@udel.edu> wrote:

Bruce Leban wrote:
And if you're worried about eval('+' + somecode), you've got three choices: (1) leave out the '+' because it has no effect
That's not true for all data types - Decimal is the one that comes to mind as having a significant use for unary '+' (specifically, it is used to say "round to currently defined precision, but otherwise leave the value alone") Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia ---------------------------------------------------------------

A complete test suite would include such a case ;-).
-1 1) This would be a petty, gratuitous restriction that would only complicate the language and make it harder to learn for no real gain. 2) It could break code. + ob maps to type(ob).__pos__(ob), which could do anything, and no necessary just return ob as you are assuming. 3) Consider eval('+' + somecode). Suppose somecode happens to start with '+'. Currently the redundancy is harmless if not meaningful. In summary, I think the following applies here: "Special cases aren't special enough to break the rules." Terry Jan Reedy

If you want to make sure that you can't use ++ or --, then target that directly: add ++ and -- as tokens to the language and make them always illegal. While that might be a bit of a kludge, I think it's far better than adding a complicated rule that you can't put two + or two - in a row. And if you're worried about eval('+' + somecode), you've got three choices: (1) leave out the '+' because it has no effect; (2) write eval('+ ' + somecode) and (3) are you sure you really want to use eval? --- Bruce On Fri, Mar 27, 2009 at 4:49 PM, Terry Reedy <tjreedy@udel.edu> wrote:

Bruce Leban wrote:
And if you're worried about eval('+' + somecode), you've got three choices: (1) leave out the '+' because it has no effect
That's not true for all data types - Decimal is the one that comes to mind as having a significant use for unary '+' (specifically, it is used to say "round to currently defined precision, but otherwise leave the value alone") Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia ---------------------------------------------------------------
participants (4)
-
Bruce Leban
-
Jared Grubb
-
Nick Coghlan
-
Terry Reedy