bugGNU Octave - Bugs: bug #51045, ?: ternary operator

 
 

bug #51045: ?: ternary operator

Submitted by:  Ernst Reissner <ernstreissner>
Submitted on:  Tue 16 May 2017 11:15:11 PM UTC  
 
Category: InterpreterSeverity: 2 - Minor
Priority: 3 - LowItem Group: Feature Request
Status: Wont FixAssigned to: None
Originator Name: ernstreissnerOpen/Closed: Closed
Release: devOperating System: Any

Add a New Comment(Rich Markup)
   

You are not logged in

Please log in, so followups can be emailed to you.

 

Fri 19 May 2017 12:15:52 PM UTC, comment #4:

Yes, I agree.
We shall close this.
It would be more desibable to convince mathworks to add the construct.

The advantage of ternary operator over merge
is just lazy evaluation.
Still I found a way to do this: passing function handles
instead of values to a function ternary(bool,handle, handle)

Ernst Reissner <ernstreissner>
Wed 17 May 2017 08:10:58 PM UTC, comment #3:

This has come up before and we've always decided against it.

For example, here is the discussion that resulted in the merge function:

http://octave.1599824.n4.nabble.com/conditional-expr-operator-in-Octave-td1636741.html

and here's an example of someone who actually added the ?: operator:

http://gnu-octave.blogspot.com/2007/11/octave-ternary-operator.html

However, even if it were a good idea to add this new syntax, I don't think we would want to use ?: syntax because the way that ':' is already used would either lead to trouble for the grammar, or at least a lot of confusion about intent, even if you worked out some precedence rules that would allow it.

But the bigger problem is that adding new syntax to Octave is not desirable because it introduces the possibility of conflicts with Matlab in the future, and those are always a lot of trouble to fix. For example, see the recent introduction of double-quoted strings in Matlab. That one will be enough fun. We don't need to add more potential future conflicts if we can avoid them.

John W. Eaton <jwe>
Project Administrator
Wed 17 May 2017 04:03:57 PM UTC, comment #2:

Yeah, merge is the Matlab compatible way to do logical selection, roughly equivalent to a ternary operator. Octave aliases this to ifelse for those familiar with Lisp.

The main problem is that this function cannot short-circuit to evaluate only one of its arguments, no function or operator overload can do that, but it is what it is. However, I suspect Ernst is looking for a short-circuiting ternary operation.

Mike Miller <mtmiller>
Project Administrator
Wed 17 May 2017 09:45:21 AM UTC, comment #1:

Might the ifelse function serve your needs?

https://www.gnu.org/software/octave/doc/interpreter/Short_002dcircuit-Boolean-Operators.html#index-ifelse

Kai Torben Ohlhus <siko1056>
Project Member
Tue 16 May 2017 11:15:11 PM UTC, original submission:

This is not really a feature request but a request for discussion:
In C there is a ternary operator:
a ? b : c
with a boolean and b and c the same but arbitrary type.
If a is true this evaluates to b else to c.

pro implementing this:
It is convenient and octave already has a lot of stuff
loaned from C.

contra implementing this:
It is another operator not present in matlab.

Another very subtile advantage:
logical operators & and | can be overwritten for objects
whereas for && and || this is (almost) impossible.
i have the case that I provide a logical type with oo means.
With the ?: ternary operator
I could write (a little simplified but just to show the idea)
bool(a) ? true : b with a, b of my own logical type
which is equivalent with a&&b.

Of course it is possible to do the same with if:
if bool(a); x=true else x=b; end
but this requires an additional auxiliary variable x.

Ernst Reissner <ernstreissner>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach File(s):
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -unavailable- added by jwe (Posted a comment)
  • -unavailable- added by mtmiller (Posted a comment)
  • -unavailable- added by siko1056 (Posted a comment)
  • -unavailable- added by ernstreissner (Submitted the item)
  • -unavailable- added by ernstreissner
  •  

    Do you think this task is very important?
    If so, you can click here to add your encouragement to it.
    This task has 0 encouragements so far.

    Only project members can vote.

     

    Please enter the title of George Orwell's famous dystopian book (it's a date):

     

     

    Follow 6 latest changes.

    Date Changed By Updated Field Previous Value => Replaced By
    Wed 17 May 2017 08:10:58 PM UTCjweStatusWorks For Me=>Wont Fix
      Open/ClosedOpen=>Closed
    Wed 17 May 2017 09:45:21 AM UTCsiko1056Severity3 - Normal=>2 - Minor
      Priority5 - Normal=>3 - Low
      StatusNone=>Works For Me
    Tue 16 May 2017 11:15:11 PM UTCernstreissnerCarbon-Copy-=>Added -unavailable-

    Back to the top


    Powered by Savane 3.1-cleanup1