The merge function is awesome!

Geoff Canyon gcanyon at gmail.com
Thu Jan 17 02:23:47 EST 2019


In Navigator I use the value() function in several ways. In particular, I
use it as a way for users to customize Navigator's list display. For some
reason I've managed to go all these years blissfully unaware of the merge()
function, which is seemingly superior to value() for this purpose in pretty
much every way. A few examples:

Using value() I use the string "tID" to represent the long id of the
control being represented. In retrospect I should have used value(<value
string>,<the long id>) which would allow the user to use "me". But I didn't
think of that when I first created the functionality, or really until just
a few minutes ago, but even so, the most basic displays are about
equivalent:

To display:
button "Cancel" - [1150]

using value():
the name of tID && "[" & the id of tID & "]"

using merge():
[[the name of tID]] - ([[the id of tID]])
(is there a way to escape the "["?)

The merge() call is cleaner, because it inherently has access to the tID
variable; for value() I have to replace "tID" with the long id before using
value().

For value(), I implemented a special iff() function, so I could do
conditionals inline. The drawback is that both of the return values get
implemented before being handed to the function, so this will fail:

get iff(1+1=2,"true",3/0) -- fails even though the value 3/0 isn't needed.

To work around that, I came up with the function iffv(), which takes
strings and then returns the value of only the necessary string. So this
will work:

get iffv("1+1=2","true","3/0") -- succeeds because the function does not
perform value("3/0")

But the syntax for that quickly becomes unwieldy. Instead, merge() supports
executing arbitrary code within it, so to display a control and its
behavior:

button "Cancel" | stack "rev_b_Cancel 5"

using value():
the name of tID & iffv(the behavior of tID is not empty,Q(" | ") && "&" &&
"the name of the behavior of" && the long id of tID,"")

using merge():
[[the name of tID]]<?if the behavior of tID is not empty then return " |"
&& the name of the behavior of tID?>

That's a lot simpler!

Merge is also possibly going to allow me to pre-calculate a single large
merge string and then use that to generate the entire HTMLtext for
Navigator's display.



More information about the use-livecode mailing list