Is there a meaningful difference in this ‘@()’ construction or is this a bug?

I have a function whose name parameter I want to provide tab completion for, the tab completion values are file names found in c:temp. On top of using the files in c:temp as values, I also want to add additional tab completion values.
Below is the function

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Function foo{
Param(
[ValidateSet([layoutNames], ErrorMessage = """{0}"" Is not a valid Layout name")]
$Name
)
$name
}
</code>
<code>Function foo{ Param( [ValidateSet([layoutNames], ErrorMessage = """{0}"" Is not a valid Layout name")] $Name ) $name } </code>
Function foo{
    Param(
    [ValidateSet([layoutNames], ErrorMessage = """{0}"" Is not a valid Layout name")]
    $Name
    )
    $name
}

and the layoutNames class, which is used by the name parameter:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Class layoutNames : System.Management.Automation.IValidateSetValuesGenerator{
[string[]] GetValidValues(){
#return @((Get-ChildItem -path 'c:temp' -File).BaseName, "valueFoo", "valueBar") #tab completetion only suggests "valueFoo" and "valueBar"
#return @("valueFoo", "valueBar", (Get-ChildItem -path 'c:temp' -File).BaseName) #tab completetion only suggests "valueFoo" and "valueBar"
return @( #tab completetion suggests "valueFoo" and "valueBar" and the file names.
"valueFoo", "valueBar"
(Get-ChildItem -path 'c:temp' -File).BaseName
)
}}
</code>
<code>Class layoutNames : System.Management.Automation.IValidateSetValuesGenerator{ [string[]] GetValidValues(){ #return @((Get-ChildItem -path 'c:temp' -File).BaseName, "valueFoo", "valueBar") #tab completetion only suggests "valueFoo" and "valueBar" #return @("valueFoo", "valueBar", (Get-ChildItem -path 'c:temp' -File).BaseName) #tab completetion only suggests "valueFoo" and "valueBar" return @( #tab completetion suggests "valueFoo" and "valueBar" and the file names. "valueFoo", "valueBar" (Get-ChildItem -path 'c:temp' -File).BaseName ) }} </code>
Class layoutNames : System.Management.Automation.IValidateSetValuesGenerator{
    [string[]] GetValidValues(){
        #return @((Get-ChildItem -path 'c:temp' -File).BaseName, "valueFoo", "valueBar")        #tab completetion only suggests "valueFoo" and "valueBar"
        #return @("valueFoo", "valueBar", (Get-ChildItem -path 'c:temp' -File).BaseName)        #tab completetion only suggests "valueFoo" and "valueBar"
        return @(                                                                                #tab completetion suggests "valueFoo" and "valueBar" and the file names.
                    "valueFoo", "valueBar"
                    (Get-ChildItem -path 'c:temp' -File).BaseName
                    )
    }}

With the above, only the third return example works, the only difference being a new line….I think.

I spent quite some time trying to figure this out, I initially started with a return statement that looked like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>return [string[]]("valueFoo", "valueBar", (Get-ChildItem -path 'c:temp' -File).BaseName)
</code>
<code>return [string[]]("valueFoo", "valueBar", (Get-ChildItem -path 'c:temp' -File).BaseName) </code>
return [string[]]("valueFoo", "valueBar", (Get-ChildItem -path 'c:temp' -File).BaseName)

but kept changing it around as nothing was working, until I eventually worked by way to using the array operator @().

The crux of my issue is that why does the class, when declared in the following manner not work as intended with the foo function, that is suggest both valueFoo, valueBar and the files names in c:temp

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Class layoutNames : System.Management.Automation.IValidateSetValuesGenerator{
[string[]] GetValidValues(){
#return [string[]]("valueFoo", "valueBar",(Get-ChildItem -path 'C:UsersINDESKAppDataRoamingGPSoftwareDirectory OpusLayouts' -File).BaseName) # no files names are suggested. only 'valueFoo' and 'valueBar' are suggested
#return [string[]]("valueFoo", "valueBar",((Get-ChildItem -path 'C:UsersINDESKAppDataRoamingGPSoftwareDirectory OpusLayouts' -File).BaseName)) # no files names are suggested. only 'valueFoo' and 'valueBar' are suggested
return [string[]](((Get-ChildItem -path 'C:UsersINDESKAppDataRoamingGPSoftwareDirectory OpusLayouts' -File).BaseName),"valueFoo", "valueBar") # no files names are suggested. only 'valueFoo' and 'valueBar' are suggested
}}
</code>
<code>Class layoutNames : System.Management.Automation.IValidateSetValuesGenerator{ [string[]] GetValidValues(){ #return [string[]]("valueFoo", "valueBar",(Get-ChildItem -path 'C:UsersINDESKAppDataRoamingGPSoftwareDirectory OpusLayouts' -File).BaseName) # no files names are suggested. only 'valueFoo' and 'valueBar' are suggested #return [string[]]("valueFoo", "valueBar",((Get-ChildItem -path 'C:UsersINDESKAppDataRoamingGPSoftwareDirectory OpusLayouts' -File).BaseName)) # no files names are suggested. only 'valueFoo' and 'valueBar' are suggested return [string[]](((Get-ChildItem -path 'C:UsersINDESKAppDataRoamingGPSoftwareDirectory OpusLayouts' -File).BaseName),"valueFoo", "valueBar") # no files names are suggested. only 'valueFoo' and 'valueBar' are suggested }} </code>
Class layoutNames : System.Management.Automation.IValidateSetValuesGenerator{
    [string[]] GetValidValues(){
        #return [string[]]("valueFoo", "valueBar",(Get-ChildItem -path 'C:UsersINDESKAppDataRoamingGPSoftwareDirectory OpusLayouts' -File).BaseName)             # no files names are suggested. only 'valueFoo' and 'valueBar' are suggested
        #return [string[]]("valueFoo", "valueBar",((Get-ChildItem -path 'C:UsersINDESKAppDataRoamingGPSoftwareDirectory OpusLayouts' -File).BaseName))               # no files names are suggested. only 'valueFoo' and 'valueBar' are suggested
        return [string[]](((Get-ChildItem -path 'C:UsersINDESKAppDataRoamingGPSoftwareDirectory OpusLayouts' -File).BaseName),"valueFoo", "valueBar")                # no files names are suggested. only 'valueFoo' and 'valueBar' are suggested
    }}

Am on pwsh 7.4/win11

1

Reason why the third example works is because there is no comma before the Get-ChildItem expression. See comma operator , details:

In expression mode, as a unary operator, the comma creates an array with just one member.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$arr = @( , (0..10))
$arr[0]
# 0
# 1
# 2
# etc
</code>
<code>$arr = @( , (0..10)) $arr[0] # 0 # 1 # 2 # etc </code>
$arr = @( , (0..10))
$arr[0]

# 0
# 1
# 2
# etc

It’s like doing:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$item = [object[]]::new(1)
$item[0] = 0..10
$arr = @($item)
$arr[0]
# 0
# 1
# 2
# etc
</code>
<code>$item = [object[]]::new(1) $item[0] = 0..10 $arr = @($item) $arr[0] # 0 # 1 # 2 # etc </code>
$item = [object[]]::new(1)
$item[0] = 0..10
$arr = @($item)
$arr[0]

# 0
# 1
# 2
# etc

If you put all expressions in a single line then you’re essentially risking creating a jagged array when Get-ChildItem outputs 2 or more items:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$arr = @(
'valueFoo', 'valueBar', (0..2)
)
$arr[2]
# 0
# 1
# 2
</code>
<code>$arr = @( 'valueFoo', 'valueBar', (0..2) ) $arr[2] # 0 # 1 # 2 </code>
$arr = @(
    'valueFoo', 'valueBar', (0..2)
)
$arr[2]

# 0
# 1
# 2

And if it returns no items then you would end up with a null element:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$arr = @(
'valueFoo', 'valueBar', (& { })
)
$arr.Count # 3 (Index 2 is `$null`)
</code>
<code>$arr = @( 'valueFoo', 'valueBar', (& { }) ) $arr.Count # 3 (Index 2 is `$null`) </code>
$arr = @(
    'valueFoo', 'valueBar', (& { })
)
$arr.Count # 3 (Index 2 is `$null`)

If you’re using the Array subexpression operator @( ) and want to put all elements in a single line, then the best you can do is to use ; as your item separator (which symbolizes a new line / new statement):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$arr = @(
'valueFoo'; 'valueBar'; (0..2)
)
$arr.Count # 5 (the third expression was enumerated)
$arr = @(
'valueFoo'; 'valueBar'; (& { })
)
$arr.Count # 2 (`AutomationNull.Value` didn't count as array element)
</code>
<code>$arr = @( 'valueFoo'; 'valueBar'; (0..2) ) $arr.Count # 5 (the third expression was enumerated) $arr = @( 'valueFoo'; 'valueBar'; (& { }) ) $arr.Count # 2 (`AutomationNull.Value` didn't count as array element) </code>
$arr = @(
    'valueFoo'; 'valueBar'; (0..2)
)
$arr.Count # 5 (the third expression was enumerated)

$arr = @(
    'valueFoo'; 'valueBar'; (& { })
)
$arr.Count # 2 (`AutomationNull.Value` didn't count as array element)

1

To add to Santiagio’s helpful answer:

Another option that avoids inadvertent creation of jagged arrays is to use array concatenation via the + operator:

  • If the LHS of a + operation is array-like (an array or a similar data type that PowerShell would auto-enumerate in the pipeline), the RHS is “appended” to the array:
    • That is, a new array is being created and returned with the RHS as additional element(s).
    • If the RHS is itself array-like, its elements become individual elements of the new array; that is, using + to “add” two flat arrays results in another flat array.

Therefore, you can do the following:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>return [string[]] (
'valueFoo', 'valueBar' + (Get-ChildItem 'c:temp' -File).BaseName
)
</code>
<code>return [string[]] ( 'valueFoo', 'valueBar' + (Get-ChildItem 'c:temp' -File).BaseName ) </code>
return [string[]] (
  'valueFoo', 'valueBar' + (Get-ChildItem 'c:temp' -File).BaseName
)

Note that, perhaps surprisingly, , (the array constructor operator) has higher precedence than +, so the LHS array ('valueFoo', 'valueBar') does not require enclosure in (...), the grouping operator.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật