Is it correct to say that in ACF, the safe operato...
# cfml-general
n
Is it correct to say that in ACF, the safe operator does two checks for null: 1. Checks the expression to the left of the operator for existence 2. Checks the variable or member function to the right of the operator for existence, if it's 1 level deep That 2nd piece is where I'm less sure. I don't see it in the docs, but it seems to work that way. For example:
a = { b = { c = {} } };
a.xyz?.c
returns
undefined
which makes sense per the docs because there's no
a.xyz
a.b?.xyz
also returns
undefined
which is looking at existence on the right side of the
?.
operator (not in the docs) •
a.b?.c.xyz
throws an error
Element c.xyz is undefined
so it seems the right side of the
?.
operator can only be undefined to 1 level •
a.b.c?.listLen()
returns
undefined
which is looking at a member function's existence (not in the docs)
d
I don't see the same behavior with
listLen()
as you do: https://trycf.com/gist/bbb49912bbe17031879c89e2717d3278/lucee5?theme=monokai The variable
c
exists, so I would expect the running of
listLen()
on it, which I would think should be
undefined
(but in the engines I tested it was just empty, which does seem weird) Basically, the things on the right side of the safe operator are going to be run, if the stuff on the left side exists
like this throws an error:
writeoutput(a.b.listLen())
which I expext
n
If you use writedump(), you can see the undefined versus blanks with writeoutput()
d
Ah, I haven't tried that, but I'd expect it to throw the same error as if you'd written the code without the safe operator
I think I saw some slight variation between the engines too, but I don't remember what it was
n
Yeah, that would make sense. I'm just using CF2023
d
Does it make sense to you why it can only be defined to one level? (Because whatever is on the right gets evaluated, so you need safeties there too if the full chain doesn't exist)
n
It was weird that it doesn't get evaluated in my 2nd example
d
In this?
a.b?.xyz
(here
b
exists, so it would try to evaluate what is on the right (
xyz
) and throw an error)
n
You would think that, but it doesnt
d
Oh, yeah, snap! That does seem wrong as well!
n
You would think the syntax would have to be
a.b.xyz?
But that's not a thing...ending with a ?
d
yeah no, it'd toss an error, since it does the logic before getting to the
?
n
Except for when it's safe navigation... e.g.
a.xyz?.c
does not error because
a
exists and
a.xyz
doesn't exist but it precedes the `?.`so it doesn't error - just returns
undefined
d
I would expect it to work like a ternary operator in that the "questionable" stuff would have to come after, regardless, but this behavior does not match that
n
So the question remains. Simpler example: https://trycf.com/gist/af46491dbff5aa039a193fe383dad5a9/acf2023 Can I rely on the behavior of the safe navigation operator in these 2 examples (at the link above) or is that unintended and could go away in the future? @Mark Takata (Adobe) Could you advise on this or point me to someone who could? The only CF docs I found on the safe navigation operator are here and they don't explicitly mention this behavior: https://helpx.adobe.com/coldfusion/using/language-enhancements.html
m
@nimitsharma can you comment here when you get a chance?