Coming from other programming languages, I would expect the following statements to all produce the same result. Can someone explain why the parenthesis make a difference here?
PS D:\> "ab", "cd"
ab
cd
PS D:\> "a"+"b" , "c"+"d"
ab cd
PS D:\> "a"+"b" , ("c"+"d")
ab cd
PS D:\> ("a"+"b"), "c"+"d"
ab
c
d
PS D:\> ("a"+"b"), ("c"+"d")
ab
cd
PS D:\>
The applicable rules are as follows:
,
is a unary or binary operator that creates an array of the operand(s), and it has higher precedence than the +
operator.+
operator can be used for numerical addition, string concatenation, and array concatenation. The data type of the left operand determines which type of operation is performed. If possible, the right operand is cast as the data type of the left operand; if not possible, an error is thrown. ** That explains seemingly inconsistent results such as that "foo" + 1
evaluates to foo1
, but 1 + "foo"
gives you an error.
So, let's take a look at your test cases, and analyze what happens following the logic explained above.
Case I:
"ab", "cd"
This one is very straightforward: the ,
operator creates an array of two strings, ab
and bc
.
Case II:
"a" + "b", "c" + "d"
This one seems counter-intuitive at a first glance; the key is to realize that ,
is evaluated before +
. Here's how the result is obtained, step by step:
"b", "c"
is evaluated first (due to ,
's precedence over +
), creating an array of the elements b
and c
.+
operator is applied to "a"
and the array created in step 1. Since the left operand "a"
is a string, a string concatenation is performed.b
and c
becomes the string b c
.a
with b c
yields ab c
.ab c
is concatenated with d
to produce the final result, ab cd
Case III:
"a"+"b" , ("c"+"d")
This yields the same result by coincidence; the evaluation occurs differently:
("c"+"d")
is evaluated first, due to the parentheses. That's a plain vanilla string concatenation, which results in the string cd
.,
operator is applied to "b"
and the string from step 1, creating an array of the elements b
and cd
. (This is evaluated before "a" + "b"
because ,
has higher precedence than +
.)+
operator is applied to "a"
and the array created in step 2. Since the left operand is a string, a string concatenation is performed.b
and cd
is cast as the string b cd
.a
and b cd
are concatenated, resulting in the final output ab cd
.Case IV:
("a" + "b"), "c" + "d"
This is the most interesting test case, and probably the most counter-intuitive, because it yields results that may appear inconsistent. Here's how it's evaluated:
("a" + "b")
is evaluated first, producing the string ab
.,
is applied to the string from step 1 and "c"
, creating an array of the elements ab
and c
.+
operator is applied to the array from step 2 and the next token, "d"
. This time, the left operand is an array, so an array concatenation is performed."d"
is cast as an array of one element.ab
, c
, and d
.Case V:
("a" + "b"), ("c" + "d")
By now, it should be easy to see what happens here:
("a" + "b")
is evaluated as the string ab
.("c" + "d")
is evaluated as the string cd
.,
is applied to the strings from steps 1 and 2, creating an array of ab
and cd
.You can see the precedence table with the command
Get-Help about_Operator_Precedence
(Help doc names can be tab-completed, so to save typing, that's help about_op
TABTAB)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With