I have a function (actually several instances of this), but there are times that it may return a list of several elements, and there are times that it may return a single element. I want the function to return an array ([System.Object[]]) every time so that (on the receiving end), I can always anticipate it being an array and index into it, even if I am just pulling the 0th element.
I've tried casting the return type multiple ways (see code below) ... including (for example) return @("asdf"), return [System.Object[]]@("asdf") and similar, but it seems that the only to get a consistent behavior is to add a second null element in the array ... which feels wrong to me. (See code below)
function fn1 {
return @("asdf")
}
function fn2 {
return [array]@("asdf")
}
function fn3 {
return [System.Object[]]@("asdf")
}
function fn4 {
# This works but with the side effect of sending a null string that is not actually necessary
return @("asdf",$Null)
}
$v = fn1 # Same for fn2, fn3.
$v.GetType().Name # Expected: Object[], Actual: String
$v[0] # Expected: "asdf", Actual: "a"
$v = fn4
$v.GetType().Name # Expected: Object[], Actual: Object[]
$v[0] # Expected: "asdf", Actual: "asdf"