docs icon indicating copy to clipboard operation
docs copied to clipboard

Document ability to use conditionals resulting in strings for GitHub Actions expressions

Open bewuethr opened this issue 2 years ago • 1 comments

Code of Conduct

What article on docs.github.com is affected?

I'm looking at https://docs.github.com/en/actions/learn-github-actions/expressions, specifically the section about operators.

What part(s) of the article would you like to see updated?

The section explains how equality comparisons are done, by casting non-matching types to numbers. For example, this expression:

A: ${{ 'abc' == 'def' && 2 || 3 }}

'abc' and 'def' have matching types, so they can be compared. The result is false, so this expression is equivalent to

A: ${{ false && 2 || 3 }}

Now, assuming && having higher precedence than ||, which I just notice isn't documented either, we compare a boolean to a number. false is coerced to 0, and 0 && 2 is... false? The bitwise logical AND of 0 and 2? And what would the overall expression evaluate to? A boolean?

In reality, the expression behaves like "if abc equals def, then return 2, else return 3", which is extremely useful, but hard to discover.

So, on a higher level, I believe this "short-circuiting" behaviour of what looks like boolean expressions actually returning literals of other types isn't documented, and on a lower, level, it's not documented what && (and ||) does if the operands aren't both booleans.

Additional information

I bumped into this behaviour via this Stack Overflow question, where the more recent and way more elegant answer takes advantage of this undocumented behaviour.

bewuethr avatar Apr 02 '23 19:04 bewuethr

@bewuethr Thanks for opening an issue and providing all this extra context 💖

I'll get this triaged for review! :zap:

cmwilson21 avatar Apr 03 '23 13:04 cmwilson21

Looks like it's been added now, here? Could be marked as "completed" instead of "not planned".

bewuethr avatar Jun 28 '23 16:06 bewuethr

@bewuethr great point. Stale bot had actually jumped on this faster than usual so it was a premature closing, anyway.

Thanks for letting us know it had been updated!

cmwilson21 avatar Jun 29 '23 17:06 cmwilson21