Say you have a struct that might or might not cont...
# cfml-general
d
Say you have a struct that might or might not contain a key you're interested in. I've always used structKeyExists() to ask if it's there. Is there a way to use the safe navigation operator ?. to accomplish something similar more concisely? This avoids blowing up assigning to x, but leaves x undefined:
Copy code
s = {a:1};
x = s?.z;
writeOutput(x);
What I'm really after is a terse equivalent of this:
Copy code
s = {a:1};
x = structKeyExists(s, "z") ? s.z :  "some other value";
writeOutput(x);
Is there any such thing?
c
x = s.z?: "some other value";
👍 1
i
You may want to use Elvis Operator. s = {a:1}; x = s.z ?: "Some Other Value";
a
Have to be a bit careful, ACF treats it as a truthy
Copy code
s = {a: false};
x = s.a ?: "Some Other Value";

writeDump( x ); // ACF returns "Some Other Value"
Lucee returns 'false'
So 'Elvis has left the building' but you have no idea if he went out the backdoor, sidedoor or beamed up by aliens
simple smile 1
d
Thanks folks, Elvis is what I was looking for, with the proviso about false-y values we've seen before an many contexts. That behavior is actually documented, as "if [the value] is defined and not null and not a false value". I actually meant to try that, but fluffed the syntax, thinking wrongly about what I needed to protect. Doh!