Just a curiosity for me.... why does this code out...
# cfml-general
s
Just a curiosity for me.... why does this code output "10" in ACF and "truefalse" in Lucee? Is it just a quirk, or is it intentional for some reason?
Copy code
<cfscript>
 myVar1 = true;
 myVar2 = 1;
 myVar3 = 0;
    
 writeoutput(myVar1 and myVar2);
 writeoutput(myVar1 and myVar3);
</cfscript>
m
huh. I'd never noticed that before.
s
the lucee one makes more sense to me personally
functionally it doesn't make much difference for me
on a similar vein (now that i am tinkering with this on trycf.com) this returns 'true' in lucee and 'YES' in ACF:
Copy code
<cfscript>
writeoutput(true eq 1);
</cfscript>
m
Now that one is a "known" thing. Apparently the Macromedia engineers were huge fans of 80s bands.
🤣 5
Oh holy smokes OK I stand corrected. They were formed in 1968? Holy moly lol.
s
I'm guessing that ACF and lucee are just taking different approaches to resolving the data type difference between 1 and true in the 'true and 1' expression
acf is turning true into 1, and lucee is turning 1 into true
but in the case of evaluating an expression, I would probably turn the 1 into true
and then evaluate the expression as true
I'll add this because it shows a little inconsistency:
Copy code
<cfscript>
 myVar1 = false;
 myVar2 = 1;
 myVar3 = 0;

 writeoutput(myVar1 or myVar2);
 writeoutput(myVar1 and myVar3);
</cfscript>
ACF = 1false Lucee = truefalse
so ACF changes it's mind when the first part of the expression is false (probably because it doesn't need to evaluate anything after the AND)
BTW, I only found this because I was doing this stupid test: https://coldfusion-skill-test.blogspot.com/ and on Q3, I selected the "TRUE" option.... which in my opinion is what it SHOULD output, but in actuallity it returns 10 in ACF Q3. What dose the following code display. <cfoutput>#(TRUE AND (5 * 2) OR 7)#</cfoutput> Options: 1. TRUE 2. 7 3. 10 4. (TRUE AND (5 * 2) OR 7) 5. TRUE10 6. False
I put it in trycf and lucee validated my correct answer of TRUE, where ACF told me I was wrong
I don't like being told I'm wrong when I don't think I am so I am complaining 🤣
why there would even be a test question like this in the first place is unclear.... I don't see any practical reason to write code like this... it feels like an intentional trick question to me
</cfrant>
m
If it makes you feel better Scott, if I ever run into the person that wrote that test, I'll give them a little kick to the shin and yell "THAT'S FOR SCOTT!". Because I'm here for the community. 🙂
👍 1
👍🏼 1
There doesn't appear to be any Abhisek in the CF slack I can torment
m
Oooh, yeah that's a shlep. Might be flying there next year though so I'll keep an eye out.
s
his whole blog consists of this one post
I hate his blog
;D
m
OH GOD I JUST CLICKED INTO THAT POST. Scott, I was having a good morning.
oh and the spelling errors too. Woo. That's a lot.
s
it's like a test of CF minutia
perhaps it was written by an egineer that didn't want his company to hire anyone else so he made sure they would mostly fail the test
I prefer to give a prospective developer a scenario with some requirements, let him write some actual code and see what they deliver
rather than do tests like this
all this would tell me is that they know how to put the code into trycf.com
Or maybe this post is some sick joke, to get OCD developers like me to spend a couple hours digging into why they got q3 wrong instead of doing the stuff they get paid to do
s
The "reason" is that
and
and
or
aren't technically Boolean operators. Several other languages have this behavior where
X and Y
has the value
X
is it is "false" in some sense (which includes
0
in CFML) or it will have the value
Y
if
X
is "true" (which includes
1
in CFML).
Similarly,
X or Y
will have the value
X
if it is "true", else it will have the value
Y
.
(I work with Clojure these days and it has the same short-circuiting behavior, returning the first value that decides overall truthy/falsey)
@Scott Bennett Would you be more or less surprised by this if the question used
&&
and
||
instead of
and
and
or
?
s
I would expect #TRUE & (5 * 2)# to output true10, and I would expect #TRUE && (5 * 2)# to output true
a
@seancorfield I even thought that was documented for CF somewhere, but I can't find it now.
s
(I could be completly wrong for expecting that, but that is what I would expect)
a
It certainly didn't surprise me, and this conversation has done the rounds here before in the last 18 months.
s
I guess I missed it
s
Yeah, it doesn't surprise me at all because several languages work this way 🙂
a
EG: JS
s
I also would never likely output anything like that so it really doesn't matter
2
a
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND
More generally, the operator returns the value of the first falsy operand encountered when evaluating from left to right, or the value of the last operand if they are all truthy.
s
Copy code
(~)-(!2063)-> node
> true && (5 * 2)
10
> false || (5 * 2)
10
>
s
both ACF and Lucee outputs will work fine in an IF where I would actually use it
1
a
Copy code
adam@DESKTOP-QV1A45U:~$ irb
irb(main):001:0> 5 and 7
=> 7
irb(main):002:0> 5 or 7
=> 5
irb(main):003:0>
Copy code
groovy:000> 5 && 7
===> true
groovy:000> 5 || 7
===> true
groovy:000>
s
Groovy is more Java-like.
a
Copy code
php > echo 5 && 7;
1
php > echo 5 || 7;
1
s
Copy code
(~/clojure)-(!2065)-> clj
Clojure 1.11.1
user=> (and true (* 5 2))
10
user=> (or false (* 5 2))
10
user=>
a
And that exhausts the languages I have on hand to test on. But, yeah, fair precedent for the behaviour. @Mark Takata (Adobe) y'all probs wanna sort yer docs out to make it clearer how it works.
s
PHP is quite C-like.
m
I put in a ticket to change all true behavior to "woof" and false to "nah". Put it in as blocker priority.
😜 1
Also "Adam Cameron hates beer" returns false.
a
Oh and just for @seancorfield
Copy code
user=> (and 5 7)
7
user=> (or 5 7)
5
(about 95% of my total Clojure skills on show there)
s
"More generally, the operator returns the value of the first falsy operand encountered when evaluating from left to right, or the value of the last operand if they are all truthy.
makes sense when explained that way.... but lucee does it the way I would intuitively expect
perhaps I have bad intuition
my wife says I have bad intuition.... I hate proving her right
all in all a bad morning for my confidence in what I expect
a
I think in a language where booleans are truthy or falsey, then there's a good case for CF's approach.
s
thanks for the detailed explanation.... 23 years in CF and I'm still learning
m
Our truthy/falsey ecosystem is a bit of a drunk toddler tbh
1
a
Me same 😄 Although not quite 23yrs.
m
Not as "interesting" as JS of course.