question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The chain operators don't work with flow-control statements exit, return, and Throw

See original GitHub issue

A common idiom (in the Bash world, which inspired PowerShell’s && and || operators) is to conditionally exit a script when invocation of a command fails, along the lines of:

# Assume existence of /somepath and exit, if it doesn't exist.
ls /somepath || exit 1

Currently, neither exit nor return nor throw can be used on the RHS of && / ||

Steps to reproduce

{ Get-Item /nosuch || return  } | Should -Not -Throw
{ Get-Item / && return  } | Should -Not -Throw

Expected behavior

The tests should pass.

Actual behavior

The tests fail, because return is not recognized

Expected no exception to be thrown, but an exception 
"The term 'return' is not recognized as the name of a 
cmdlet, function, script file, or operable program. ..."...

Environment data

PowerShell Core 7.0.0-preview.6

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:5
  • Comments:33 (14 by maintainers)

github_iconTop GitHub Comments

4reactions
mi-holcommented, Nov 3, 2019

I wonder how the fact “syntactically it doesn’t work because the big difference between PowerShell and bash” can we made clear to users. I had assumed that this new feature would basically work the same in PowerShell as in bash, hence many others will very likely start with the same assumption. At least this conclusion seems ‘another nail in the coffin’ for the goal ‘pwsh is a replacement to bash’ 😦

2reactions
mklement0commented, Nov 24, 2019

@rjmholt

Thank you for putting up a good fight and for laying out the rationale.

Before I even try to understand the intricacies of the grammar, let me make a plea purely from a user’s perspective:

If we force the use of $(...):

  • We burden what is probably the primary use case for && and ||.

  • New users will not expect the need for $(...) and won’t necessarily know that it is what is required when foo || exit 1 fails (though documentation will help there).

  • Even once users do know about the need for $(...):

    • They will forget to use it on occasion, because of the counter-intuitive need for it.
    • When they do remember, this seemingly artificial requirement will be an ongoing source of frustration, especially since $(...) is hard to type.

As an aside: The same arguments apply to the unfortunate decision to require {...} around identifiers in order to be able to use the null-conditional member-access operator, ?..

So, for the sake of the end-users’ experience, can we look for a solution that works as expected, is easy to explain, and technically not too tortu[r]ous?

Personally, I’m not worried about cases such as Get-Item ./doesntexist || throw "More things" && "other stuff" &, because I wouldn’t expect many people to attempt such acrobatics.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Control flow and error handling - JavaScript - MDN Web Docs
throw statement. Use the throw statement to throw an exception. A throw statement specifies the value to be thrown: js
Read more >
Are exceptions as control flow considered a serious ...
Exceptional situations are handled by the raise / rescue operators. But you can use throw / catch for exception-like control flow constructs**.
Read more >
c++ - How to avoid "if" chains?
The problem with the guard pattern is that it relies on what is called "opportunistic return" or "opportunistic exit." In other words, it ......
Read more >
Control Flow - Julia Documentation
Julia provides a variety of control flow constructs: ... This value is simply the return value of the last executed statement in the...
Read more >
Control Flow - Documentation - Swift.org
Structure code with branches, loops, and early exits. ... Swift provides a variety of control flow statements. ... Use the half-open range operator...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found