Skip to main content
Inspiring
April 25, 2022
Question

Elvis Operator Not Working

  • April 25, 2022
  • 3 replies
  • 1134 views
<cfscript>
    message = [];
    test = true;
    response = test ? ['It is true'] : ['It is not true'];
</cfscript>

CF2021 Throws the following error:

 

ColdFusion was looking at the following text:<p>:</p><p>The CFML compiler was processing:<ul><li>A cfscript tag beginning on line 1, column 2.</ul>

 

Used to work in CF2016 Patch #17

    This topic has been closed for replies.

    3 replies

    James Moberg
    Inspiring
    April 26, 2022

    Fore reference, here's an observation from Ben Nadal's blog regarding behavior differences between Adobe ColdFusion and Lucee..
    https://www.bennadel.com/blog/4231-fundamental-differences-in-elvis-operator-between-adobe-coldfusion-and-lucee-cfml.htm

    "the Elvis operator is basically unusable in frameworks like ColdBox (which have to be cross-engine compatible) due to the number of issues that it introduces."

    BKBK
    Community Expert
    April 26, 2022

    Let's first clear up a misunderstanding. This concerns the ternary operator, not the Elvis operator.

     

    In any case, I think it is a bug in CF2021. I have reported it: https://tracker.adobe.com/#/view/CF-4213245 

     

    Two workarounds:

     

    (1)
    <cfscript>
        test=true;
        response = #test# ? ['It is true'] : ['It is not true'];
        writedump(response);
    </cfscript>
    
    (2)
    <cfset test=true>
    <cfset response = (test) ? ['It is true'] : ['It is not true']>
    <cfdump var="#response#">

     

     

     

    Charlie Arehart
    Community Expert
    April 26, 2022

    Good catch, bkbk. Like Jojo, I can get them confused when hurrying. And that's even better on news on your having opened a ticket.

     

    And I'd have added a vote, but curiously the vote button isn't visible via mobile (in Chrome on Android). I'd not noticed that before. I wonder if it's because of the attachment offered. Oh well. Hope to do it later on desktop. 

     

    Jojo, you'll want to also of course. I realize you've already explained why workarounds will be painful. 

    /Charlie (troubleshooter, carehart. org)
    Charlie Arehart
    Community Expert
    April 25, 2022

    Before anyone may propose that this is a problem calling for removal of the compiled java class created by his code (which was a problem after a certain recent update, for existing templates that used the elvis operator), that's NOT Jojo's issue here.

     

    You can test code in cffiddle.org and see that indeed it will fail. (And no, it will not fail with Lucee, as you can see with trycf.com, which also supports ACF.)

     

    The problem is in the use of an array elements on line 4. I agree that it logically should work to use implicit array notation there (just like it would work if you assigned one of those to the first message variable, which seems unneeded in this code).

     

    Let's see if Adobe or anyone else may chime in with news on this, or perhaps a workaround.

     

    Well, I mean a better workaround than this, which seems obvious enough and works but is likely unsatisfying for your actual need. 🙂

     

     

     

    <cfscript>
        message1 = ['It is true'];
        message2 = ['It is not true'];
        test = true;
        response = test ? message1 : message2;
        writedump(response);
    </cfscript>

     

     

     

    One last thing: I tried your code in cffiddle which ALSO offers running code at CF2016 update 17, and it does not work there. Perhaps you meant it worked BEFORE update 17. (My bad: I had a different error--a missing semi-colon on a writedump line I'd added--which had caused the failure in CF2016 u17, in my test on cffiddle.org.)

    /Charlie (troubleshooter, carehart. org)
    --jojo--Author
    Inspiring
    April 25, 2022

    @Charlie Arehart 

    We're currently on CF2016 update 17 and no, it's not an issue - it currently works - we have plenty of. Yes, we could rewrite them, but there's no easy way of searching for all instances/permutations of the logic. I've already replaced a dozen occurences, I'm not about to go searching for more if I could avoid it.

    Charlie Arehart
    Community Expert
    April 26, 2022

    Yep, my bad on saying it worked in CF2016 u17. More on that in a moment.

     

    And to be clear, I certainly wasn't proposing you should change any code. I said I'd agreed that it should work. I merely mentioned a workaround and that others may try to offer one. But I would agree that past code that worked should continue (unless there's a reason by Adobe that it should no longer be compatible).

     

    And really, you should go ahead and file a bug report, at tracker.adobe.com. If you do that, share the link and/or ticket number, so other folks interested can add a vote.

     

    Finally, as for my mistake in the last paragraph of my first reply, I did just correct that comment, striking out that last paragraph. As I noted there, the problem was that when I added some additional code (for when things did work elsewhere), I had forgotten the semi-colon. While CF2018 made that optional, CF2016 still requires it. I  failed to notice the error message had changed. 

     

    Anyway, I hope the rest of what I added may help folks, in helping you.

    /Charlie (troubleshooter, carehart. org)