Elvis operator not working on application scope or session

I am finding that the Elvis operator does not work against session or application scopes.

url.errorCode = url.errorCode ?: 200;

This works but

application.flip = !application.flip ?: false;

Does not. I have to add

    if (not isDefined("application.flip")) {

        application.flip = false;   

    }

I have tried it locally under command box with latest Lucee 5 and on server under IIS and various versions of Lucee 5 all with same results.

Is this a known issue / intended outcome?

Wrap the Application part in some paranthesis…

// This doesn't work
application.flip = !application.flip ?: false;

// This does
application.flip = !(application.flip ?: false);
1 Like

Cheers that worked, should it be reported as a bug so it gets fixed long term?

I’m not exactly sure what you are trying to do?

As both of them will error out if the key doesn’t exist or if the value is a string (with the exclamation mark included):

url.errorCode = !url.errorCode ?: 200; // key [ERRORCODE] doesn't exist

session.flip = !session.flip ?: false; // key [FLIP] doesn't exist

session.flip = !'text' ?: false; // Can't cast String [text] to a boolean

My assumption is that you try to set a value if it doesn’t exist.

One way could be with cfparam (<cfparam> :: Lucee Documentation)
Another one would be to use a ternary (Operators :: Lucee Documentation):

session.flip = session.keyExists('flip') ? session.flip : false;

session.flip = !isNull(session.flip) ? session.flip : false;

session.flip = session.keyExists('flip') && isBoolean(session.flip) ? session.flip : false;

I believe here it would need to have two exclamation marks, otherwise it might be a toggle, then again maybe this was the point / what the person wanted.

dump(session.keyExists('flip') ? session.delete('flip') & ' flip deleted' : 'flip not exist');
dump(var=session.flip = !(session.flip ?: false)); // true
dump(var=session.flip = !(session.flip ?: false)); // false
dump(var=session.flip = !(session.flip ?: false)); // true

dump(session.keyExists('flip') ? session.delete('flip') & ' flip deleted' : 'flip not exist');
dump(var=session.flip = !!(session.flip ?: false)); // false
dump(var=session.flip = !!(session.flip ?: false)); // false
dump(var=session.flip = !!(session.flip ?: false)); // false