I'm trying to make a simple "coalesce" function but struggling with Powershell return syntax
Attempts:
function coalesce {
foreach($item in $args) {
if ($item) {
return $item
break # also tried continue and exit
}
}
}
coalesce($nil,2,3)
2
3
function coalesce {
ForEach-Object -InputObject $args { If($_) { return $_; exit } } # also tried continue and break
}
coalesce($nil,2,3)
2
3
So how do I simulate a return
inside a loop that exits the function in any other programming language?