Git Product home page Git Product logo

Comments (2)

MartinGC94 avatar MartinGC94 commented on June 1, 2024 1

It's because of your return statement. You can't assign the value of a return statement to a variable, even if you wrap it in a subexpression. See the following demonstration:

$Res = return "Hello"
$Res | Out-Host
$Res = $(return "Hello")
$Res | Out-Host
$Res = "Hello"
$Res | Out-Host

This behavior is consistent with what I see in 5.1 so I doubt 7.3 behaved differently. Most likely you were just hitting the branch without the return statement when you were using 7.3.

from powershell.

dbrennand avatar dbrennand commented on June 1, 2024

@BasBerk @MartinGC94

I've also seen this behavioural change. We noticed this from one of our Pester tests failing in our CI. I believe this issue is related to the sub-expression operator.

Seems to be that PowerShell versions 7.4.0-rc.1, 7.4.1, 7.5.0-preview.2 are affected. Interestingly, PowerShell 7.4.2 is not affected.

Take the simple example below, using the sub-expression operator:

function Get-Planet {
    [CmdletBinding()]
    param (
        $Planet = $(
            return "Earth"
        )
    )
    
    process {
        Write-Output -InputObject "Planet is $Planet"
    }
}
Get-Planet

PowerShell 5.1:

PS > $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.22000.2713
...


PS > function Get-Planet {
>>     [CmdletBinding()]
>>     param (
>>         $Planet = $(
>>             return "Earth"
>>         )
>>     )
>>
>>     process {
>>         Write-Output -InputObject "Planet is $Planet"
>>     }
>> }
PS > Get-Planet
Planet is Earth

PowerShell 7.3.9

docker run -it mcr.microsoft.com/powershell:7.3-ubuntu-22.04
PowerShell 7.3.9
PS /> function Get-Planet {
>>     [CmdletBinding()]
>>     param (
>>         $Planet = $(
>>             return "Earth"
>>         )
>>     )
>>
>>     process {
>>         Write-Output -InputObject "Planet is $Planet"
>>     }
>> }
PS /> Get-Planet
Planet is Earth

PowerShell 7.4.0-rc1

docker run -it mcr.microsoft.com/powershell:preview-7.4-ubuntu-22.04
PowerShell 7.4.0-rc.1
PS /> function Get-Planet {
>>     [CmdletBinding()]
>>     param (
>>         $Planet = $(
>>             return "Earth"
>>         )
>>     )
>>
>>     process {
>>         Write-Output -InputObject "Planet is $Planet"
>>     }
>> }
PS /> Get-Planet
Planet is

PowerShell 7.4.1

PS > $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.4.1
...

PS > function Get-Planet {
>>     [CmdletBinding()]
>>     param (
>>         $Planet = $(
>>             return "Earth"
>>         )
>>     )
>>
>>     process {
>>         Write-Output -InputObject "Planet is $Planet"
>>     }
>> }
PS > Get-Planet
Planet is

PowerShell 7.4.2

docker run -it mcr.microsoft.com/powershell:7.4-ubuntu-22.04
PowerShell 7.4.2
PS /> function Get-Planet {
>>     [CmdletBinding()]
>>     param (
>>         $Planet = $(
>>             return "Earth"
>>         )
>>     )
>>
>>     process {
>>         Write-Output -InputObject "Planet is $Planet"
>>     }
>> }
PS /> Get-Planet
Planet is Earth

PowerShell 7.5.0-preview.2

docker run -it mcr.microsoft.com/powershell:preview-7.5-ubuntu-22.04
PowerShell 7.5.0-preview.2
PS /> function Get-Planet {
>>     [CmdletBinding()]
>>     param (
>>         $Planet = $(
>>             return "Earth"
>>         )
>>     )
>>
>>     process {
>>         Write-Output -InputObject "Planet is $Planet"
>>     }
>> }
PS /> Get-Planet
Planet is

from powershell.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.