Well, it's finally happened. ColdFusion's type coe...
# cfml-general
p
Well, it's finally happened. ColdFusion's type coercion finally got me. I was checking to see if the last two characters equaled "-0" and the following evaluated true: if (right("Dan Marino",2) == '-0') {}
🤣 2
b
You're probably already aware, but you can use
compareNoCase()
to force a string comparison
But yes, this is the seedy underbelly of languages with a flexible system of "truthy" values and a generic equality operator which auto-casts
It's quite handy, until it isn't
p
Thanks Brad! Yeah, I just thought I'd share.
b
We've hit this a lot writing BoxLang. CF's penchant for treating dates as numbers has to be the worst. There's a few specific scenarios it makes a great deal of sense
Copy code
now() + 7 // Next week
and a huge amount of places it really doesn't make sense
Copy code
now().abs()
So you either end up with an inconsistent language that treats dates as numbers, but only on Tuesdays after 5 PM, or unexpected behaviors and vague member methods which don't do what you'd expect.
t
now().abs() == now() as long as now > epoch? (just trying to guess what this would actually do....)
b
heh, well
abs()
is simply a BIF which can be called as a member method on any numeric value
Substitute any numeric member method you like
Just because a date can be used as a number in some cases, doesn't mean you'll be getting the co-tangent of yesterday 🙂
t
of course
b
And then, even worse, there can be collisions between member methods of different types.
leaving ambiguity about what member method you should really be calling when a given variable can behave as more than one type at the same time.