Modify

Opened 11 years ago

Closed 11 years ago

Last modified 10 years ago

#2750 closed Feature Request (Rejected)

Ternary Operator Should Have the Lowest Precedence

Reported by: anonymous Owned by:
Milestone: Component: AutoIt
Version: Severity: None
Keywords: ternary, operator precedence Cc:

Description

The ternary operator should have the lowest precedence in expressions. That allows the use of And and Or in the first part of a ternary expression. That is far more likely than the conditional evaluation of a truth value combined with And or Or.
It also doesn't confuse programmers who are used to that precedence.
Example:

ConsoleWrite (True Or False ? evalZero () : evalOne ())
; "True", not "2"; evalZero is not called

This operator is missing from the operator precedence list in the documentation. Please add it at the bottom.

Attachments (0)

Change History (12)

comment:1 follow-up: Changed 11 years ago by TicketCleanup

  • Version 3.3.12.0 deleted

Automatic ticket cleanup.

comment:2 follow-ups: Changed 11 years ago by Melba23

You can already use And & Or inside the first part of a ternary expression:

$a = 1
$b = 0
$c = 1

$sResult = ( ($a And $b) ? ("True") : ("False") )
ConsoleWrite($sResult & @CRLF)

$sResult = ( ($a And $c) ? ("True") : ("False") )
ConsoleWrite($sResult & @CRLF

So what exactly is your request?

M23

comment:3 in reply to: ↑ 2 ; follow-up: Changed 11 years ago by anonymous

Replying to Melba23:

You can already use And & Or inside the first part of a ternary expression

Sorry for being inaccurate. It should have been: "That allows the use of an and-expression and or-expression as the first part of a conditional-operator-expression (without a wrapping parentheses-expression)." Better?

So what exactly is your request?

A change of the operator precedence for less necessary parentheses and less confusion. The first part of a ternary expression would never need to be a parentheses expression (assuming the use of == for comparison in some special cases).

comment:4 follow-ups: Changed 11 years ago by Melba23

  • Resolution set to Rejected
  • Status changed from new to closed

Much clearer thanks.

Personally I do not regard the ternary operator as a true operator, more as a condensed If (in fact it replaced the _IIf UDF). So I not believe that it has a precedence as such and the use of parentheses to make sure that the various expressions within the ternary operator are correctly parsed is entirely proper and necessary. I would consider it a parallel with the requirement to use parentheses with Not on occasion to ensure that only the correct element is affected.

We clearly say in the Help file:

Although not necessary in all cases, it is strongly recommended that the 3 elements are enclosed in parentheses.

This remark was added to cater for cases where operator precedence needed to be specified - such as the one you have mentioned above. Furthermore, I fail to see why not having parentheses would produce "less confusion" - to my mind it is very likely to create more as the various elements of the ternary syntax become less distinct.

So I am rejecting this request.

M23

comment:5 in reply to: ↑ 4 Changed 11 years ago by anonymous

Replying to Melba23:

Furthermore, I fail to see why not having parentheses would produce "less confusion" - to my mind it is very likely to create more as the various elements of the ternary syntax become less distinct.

Less confusion though less unexpected results. The documentation right now doesn't help. Most programmers will think, if they went as far as reading the page about the conditional operator: "Why should I use parentheses? I never did, and I can still understand it. There is no reason for me to respect that remark: It doesn't illustrate the necessarity (Who dares to criticize my programming style?), and I know the operator precedence of C and so many other languages, I won't even think about the possibility that somebody might risk changing it." Is it a good idea to irritate them with something so simple?

comment:6 follow-up: Changed 10 years ago by wwvpusox <sample@…>

1

comment:7 in reply to: ↑ 3 Changed 10 years ago by wwvpusox <sample@…>

1

comment:9 in reply to: ↑ description Changed 10 years ago by wwvpusox <sample@…>

1

comment:8 in reply to: ↑ 1 Changed 10 years ago by wwvpusox <sample@…>

1

comment:9 in reply to: ↑ 6 Changed 10 years ago by wwvpusox <sample@…>

1

comment:10 in reply to: ↑ 4 Changed 10 years ago by wwvpusox <sample@…>

1

comment:11 in reply to: ↑ 2 Changed 10 years ago by wwvpusox <sample@…>

1

Guidelines for posting comments:

  • You cannot re-open a ticket but you may still leave a comment if you have additional information to add.
  • In-depth discussions should take place on the forum.

For more information see the full version of the ticket guidelines here.

Add Comment

Modify Ticket

Action
as closed The ticket will remain with no owner.
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.