Jump to content


Photo

a .= (condition ? a [: b])


  • Please log in to reply
11 replies to this topic

Poll: Do you like the idea (8 member(s) have cast votes)

Do you like the idea

  1. Yes (4 votes [50.00%])

    Percentage of vote: 50.00%

  2. No (4 votes [50.00%])

    Percentage of vote: 50.00%

Vote Guests cannot vote

#1 lordkrandel

lordkrandel
  • Members
  • 31 posts

Posted 01 March 2011 - 04:54 PM

What about making third part of the ternary operator optional with a null default ("") ?

v .= (condition ? "a" )
Add "a" only if condition, do nothing otherwise.

#2 HotKeyIt

HotKeyIt
  • Fellows
  • 6132 posts

Posted 01 March 2011 - 06:48 PM

Already possible:
MsgBox % v .= true [color=red]?[/color] "a" [color=red]:[/color]


#3 Lexikos

Lexikos
  • Administrators
  • 8845 posts

Posted 01 March 2011 - 09:41 PM

It may appear to work in some cases, but only because AutoHotkey handles stack underflow gracefully (i.e. it silently aborts the expression). It does not, in fact, work in this case.
v := "This is not displayed"
MsgBox % v .= false ? "a" :
It is impossible to use only two operands with the ternary operator, by definition. (x ? y) would be a binary operation.

#4 HotKeyIt

HotKeyIt
  • Fellows
  • 6132 posts

Posted 01 March 2011 - 09:57 PM

Thanks Lexikos for pointing out, again something learned :D

#5 lordkrandel

lordkrandel
  • Members
  • 31 posts

Posted 02 March 2011 - 09:19 AM

I'm using this at the moment

my_if(k,v) {
    return (k ? v : "")
}

modifiers := ""
     . my_if(vCtrl, "Ctrl")
     . my_if(vAlt, "Alt")
     . my_if(vShift, "Shift")
     . my_if(vWin, "Win")
hotkey := my_if(modifiers, modifiers "+") hotkey


#6 RaptorX

RaptorX
  • Members
  • 717 posts

Posted 02 March 2011 - 02:49 PM

you can safely say:
    return (k ? v [color=red]:[/color])

if you dont like adding "".

I personally have a variable called null that i always set to "".
that way I say:

    return (k ? v : null)

Which is kind of more appealing.

I personally would like if the variable null was like the variables true and false. That is that its content cant be changed. That way i could use that expression in any script without having to make sure that it is really empty.

#7 jaco0646

jaco0646
  • Fellows
  • 3163 posts

Posted 02 March 2011 - 05:45 PM

RaptorX, please read the thread. Your initial code is not safe. I like the idea of a null constant for readability, though.

#8 RaptorX

RaptorX
  • Members
  • 717 posts

Posted 02 March 2011 - 06:21 PM

No, I gave a correct answer.

His return code simply checks if the variable k is empty, and returns v or nothing which i am positive my initial code does without problems.

Keep in mind that the code i recommended is in the context of his "my_if" function which is his creative solution to this algorithm:

_mhk:=(_ctrl ? "^" : null)(_alt ? "!" : null)(_shift ? "+" : null)(_win ? "#" : null)(_hkddl = "None" ? "#``" : _hkddl) ; main hotkey

I use the above code for saving a hotkey from a GUI.
Note that if you dont use null or "" or simply : the above does not work. You cannot simply say: (_ctrl ? "^")

I could easily say using his method:
_mhk:= check(_ctrl, "Ctrl") check(_alt, "Alt") check(_shift, "Shift") check(win, "Win") (_hkddl = "None" ? "#``" : _hkddl)
check(mod, set){
    return mod ? set :             ; I dont use parentheses unless strictly necessary like above.
}

Which would work as you expect it to work.
But as Lexikos did point out, that is not always the case. There are some special cases, like the one he showed:

v := "This is not displayed"
MsgBox % v [color=red].=[/color] false ? "a" :     ; this wouldnt append... it behaves as if you used ":=" instead

Curiously if you use true in the above code it does append normally.

#9 Lexikos

Lexikos
  • Administrators
  • 8845 posts

Posted 02 March 2011 - 10:24 PM

If the false branch is missing and the condition evaluates to false, one of two things happen:
[*:3909f2yg]If the stack is empty, the expression is aborted and it yields an empty string.
[*:3909f2yg]Otherwise, the wrong value is popped off the stack; probably one meant for the next operation. The next operation will thus also pop the wrong value off the stack, and so on until the stack is emptied prematurely and the expression is aborted.Since an aborted expression evaluates to "", in some cases it will appear correct. Although it may work, it is not supported, and therefore should not be used.

#10 jaco0646

jaco0646
  • Fellows
  • 3163 posts

Posted 02 March 2011 - 10:49 PM

Same here?
v := "This is not displayed"
MsgBox % v .= true ?  : "a"


#11 Lexikos

Lexikos
  • Administrators
  • 8845 posts

Posted 03 March 2011 - 01:08 AM

Yes. There are also other syntax errors which cause stack underflow, such as 2-/1.

#12 lordkrandel

lordkrandel
  • Members
  • 31 posts

Posted 09 March 2011 - 08:15 AM

That could also be a ||= b, as in other languages