zackster
05/15/2025, 4:03 PMvar prop = some?.key?.that?.is?.not?.there ?: "I'm a default value!";
https://docs.lucee.org/guides/developing-with-lucee-server/operators.html#safe-navigation-operatorbdw429s
05/15/2025, 5:43 PMbdw429s
05/15/2025, 5:44 PMbdw429s
05/15/2025, 5:44 PMzackster
05/15/2025, 5:49 PMbdw429s
05/15/2025, 5:49 PMbdw429s
05/15/2025, 5:50 PMbdw429s
05/15/2025, 5:52 PMfoo = "foo"
writedump( foo.toUpperCase() ) // FOO
writedump( foo?.toBradCase() ?: "just kidding" ) // just kidding
most people forget thatbdw429s
05/15/2025, 5:52 PMzackster
05/15/2025, 5:53 PMbdw429s
05/15/2025, 5:55 PMbdw429s
05/15/2025, 5:56 PMfoo = "foo"
writedump( foo?.bar ?: "default" )
Lucee and Boxlang both work fine, but Adobe complains about treating trying to dereference a key on a stringbdw429s
05/15/2025, 5:57 PMzackster
05/15/2025, 5:58 PMbdw429s
05/15/2025, 6:00 PMbdw429s
05/15/2025, 6:00 PMbdw429s
05/15/2025, 6:01 PMzackster
05/15/2025, 6:05 PMBK BK
05/16/2025, 1:06 PM// Newsflash
if (rand() gt 0.5) {
LasVegasWestgateHotel.penthouseSuite.ElvisLounge.confirmedSighting="Elvis is in the building!";
} else {
LasVegasWestgateHotel.penthouseSuite.ElvisLounge.unConfirmedLocation=true;
}
writeoutput(LasVegasWestgateHotel?.penthouseSuite?.ElvisLounge?.confirmedSighting?:"Elvis has left the building!");bdw429s
05/16/2025, 5:22 PMLasVegasWestgateHotel.penthouseSuite.ElvisLounge.confirmedSighting ?: "Elvis has left the building!"
☝️ behaves just the samebdw429s
05/16/2025, 5:23 PMsafe flag in our AST for all "access" nodes on its left hand side, which is the same flag that the safe nav operator flips.zackster
05/16/2025, 5:51 PMbdw429s
05/16/2025, 5:52 PMzackster
05/16/2025, 5:52 PMzackster
05/16/2025, 5:57 PMBK BK
05/16/2025, 6:34 PM<p><button onclick="window.location.reload();">NewsFlash</button></p>
<cfscript>
if (rand() gt 0.5) {
LasVegasWestgateHotel.penthouseSuite.ElvisLounge.confirmedSighting="Elvis is in the building!";
} else {
LasVegasWestgateHotel.penthouseSuite.ElvisLounge.unConfirmedLocation=true;
}
writeoutput(LasVegasWestgateHotel?.penthouseSuite?.ElvisLounge?.confirmedSighting?:"Elvis has left the building!");
</cfscript>BK BK
05/16/2025, 6:44 PMbdw429s
05/16/2025, 6:45 PMbdw429s
05/16/2025, 6:45 PMBK BK
05/16/2025, 7:07 PMbdw429s
05/16/2025, 7:08 PMbdw429s
05/16/2025, 7:08 PMbdw429s
05/16/2025, 7:09 PMfoo={}
dump( foo?.bar ?: "default" ) // default
dump( foo.bar ?: "default" ) // default
both versions produce the same resultbdw429s
05/16/2025, 7:09 PMbdw429s
05/16/2025, 7:10 PMBK BK
05/16/2025, 7:34 PMany expression with elvis at the end, doesn't need to use safe navigation.
Me
an expression with elvis at the end, may use safe navigation.
(the reason bring that "?." says something syntactically different from ".", which was a consideration in my use-case)
Now that we understand each other, I hope we can agree to differ.bdw429s
05/16/2025, 7:36 PMbdw429s
05/16/2025, 7:36 PMbdw429s
05/16/2025, 7:36 PMBK BK
05/17/2025, 10:27 AM!!true and true have the same end-result, you may use them to express different things. So, too, foo?.bar ?: "default" and foo.bar ?: "default" . For example, the safe-navigation question marks in LasVegasWestgateHotel?.penthouseSuite?.ElvisLounge?.confirmedSighting?:"Elvis has left the building!", though redundant, help convey the idea of a search for the great man across the lobbies.
Lucee, like every language known to humanity, has redundancy. Lots of it, for a reason. That enables you to express the same thing in different ways, depending on the meaning you wish to convey and on the way in which you wish to convey it. For example, you mention readability, which I wholeheartedly agree with.