News for hackers. Stuff that matters.
Re:Something about functions in Bash (2017) by maxxxxx on Thursday July 05, 2018 @03:58PM (#17466305)
I gave up on PowerShell when I found out that the return value of a function is the console output. How s....d is that?
Re:Something about functions in Bash (2017) by jodrellblank on Friday July 06, 2018 @12:08AM (#17468766)
not stupid. Imagine if you wrote:

    get-content file.txt
and the result was the file content visible on screen. Then you were happy with your code and you put it in a function for reuse:

    function test {
        get-content file.txt
    }
and now there's no output. PowerShell pipeline is not stdout, get-content doesn't display anything on screen, only the output formatters at the end of the pipeline do. If the function has no output by default then you would see nothing. That would be an annoying behaviour the other way. In PS, braces {} make anonymous codeblocks and they're used often, e.g. in code like

    # square the first 5 numbers
    1..5 | foreach { $_ * $_ }

    # rename some files
    Get-ChildItem | where { $_.Name -notmatch '[0-9]' } | Rename-Item -NewName { $_.Name + "2" }
That would be really annoying if you always had to 'return' results out of scriptblocks. And it would be annoying if scriptblocks in functions behaved differently to scriptblocks in filterscripts or calculated properties.
Re:Something about functions in Bash (2017) by reificator on Thursday July 05, 2018 @04:29PM (#17466550)
Yeah that's fair.

You can work around it, but you really shouldn't have to.

Re:Something about functions in Bash (2017) by maxxxxx on Thursday July 05, 2018 @04:38PM (#17466616)
The common workaround is to pass in a dictionary that's then used for return values. Workable, but seriously? Is that the best they could do?
Re:Something about functions in Bash (2017) by jodrellblank on Friday July 06, 2018 @12:33AM (#17468850)
Do you have any examples? This doesn't sound familiar.

Dictionaries are often used for return values, as they can be cast to PSCustomObject for a quick way to make objects for the pipeline. But in that use case they aren't passed in, and that's not anything to do with the `return` keyword or all output from all commands becoming function output.

Re:Something about functions in Bash (2017) by reificator on Thursday July 05, 2018 @05:43PM (#17467100)
That is... not the way I generally work around it or see it.

Normally you just use Write-Host when you need it and otherwise just collect return values inside the function by assigning them to variables.

It's basically solved if you never call a command without storing the return value somewhere. Which is not optimal in a scripting language where you just want to get things done, but it's not the end of the world.