Skip to main content
added 636 characters in body
Source Link
kaya3
  • 22.4k
  • 50
  • 139

There are a couple of arguments in favour of the x[y] syntax for arrays, over an infix operator.

One is that we want array access to bind the left operand more tightly than the right operand. For example, in x + y[z] we want the access to have higher precedence than +, but in x[y + z] we want the opposite. An infix operator normally has the same precedence on both sides.

Another is that infix operators need to decide on associativity. Neither of x[y][z] or x[y[z]] is clearly a better choice in general for the meaning of x @ y @ z. Probably multidimensional array access x[y][z] is more commonly used, but left-associativity would mean the operator binds slightly looser on the left, contradicting the above. (That is, if @ is left-associative then x @ y binds where y is the right operand, instead of y @ z where y is the left operand.)

Both of these reasons can be summarised by saying that we would often need to use parentheses anyway to disambiguate usages of an infix array access operator; so we might as well make the parentheses part of the syntax, and then we don't need the additional symbol for an infix operator.


Another thing to consider is that in some languages, the "index" in an array access is not necessarily an expression. For example, many languages allow access to multidimensional arrays using commas like x[y, z] even when y, z is not a valid expression in those languages. There are also array slices like Python's x[y:z] or x[::-1], where y:z and ::-1 are not expressions.

In such languages, array access cannot be parsed the same way as a binary infix operator which expects expressions for both operands. However, languages which want to use an infix operator could get around this by allowing tuples and slices to be expressions.

There are a couple of arguments in favour of the x[y] syntax for arrays, over an infix operator.

One is that we want array access to bind the left operand more tightly than the right operand. For example, in x + y[z] we want the access to have higher precedence than +, but in x[y + z] we want the opposite. An infix operator normally has the same precedence on both sides.

Another is that infix operators need to decide on associativity. Neither of x[y][z] or x[y[z]] is clearly a better choice in general for the meaning of x @ y @ z. Probably multidimensional array access x[y][z] is more commonly used, but left-associativity would mean the operator binds slightly looser on the left, contradicting the above. (That is, if @ is left-associative then x @ y binds where y is the right operand, instead of y @ z where y is the left operand.)

Both of these reasons can be summarised by saying that we would often need to use parentheses anyway to disambiguate usages of an infix array access operator; so we might as well make the parentheses part of the syntax, and then we don't need the additional symbol for an infix operator.

There are a couple of arguments in favour of the x[y] syntax for arrays, over an infix operator.

One is that we want array access to bind the left operand more tightly than the right operand. For example, in x + y[z] we want the access to have higher precedence than +, but in x[y + z] we want the opposite. An infix operator normally has the same precedence on both sides.

Another is that infix operators need to decide on associativity. Neither of x[y][z] or x[y[z]] is clearly a better choice in general for the meaning of x @ y @ z. Probably multidimensional array access x[y][z] is more commonly used, but left-associativity would mean the operator binds slightly looser on the left, contradicting the above. (That is, if @ is left-associative then x @ y binds where y is the right operand, instead of y @ z where y is the left operand.)

Both of these reasons can be summarised by saying that we would often need to use parentheses anyway to disambiguate usages of an infix array access operator; so we might as well make the parentheses part of the syntax, and then we don't need the additional symbol for an infix operator.


Another thing to consider is that in some languages, the "index" in an array access is not necessarily an expression. For example, many languages allow access to multidimensional arrays using commas like x[y, z] even when y, z is not a valid expression in those languages. There are also array slices like Python's x[y:z] or x[::-1], where y:z and ::-1 are not expressions.

In such languages, array access cannot be parsed the same way as a binary infix operator which expects expressions for both operands. However, languages which want to use an infix operator could get around this by allowing tuples and slices to be expressions.

deleted 201 characters in body
Source Link
kaya3
  • 22.4k
  • 50
  • 139

I think it's worth clarifying that array access is a binary operator, since it has two operands; it is (usually) not an infix operator, i.e. it is not a single token written between the operands.

There are a couple of arguments in favour of the x[y] syntax for arrays, over an infix operator.

One is that we want array access to bind the left operand more tightly than the right operand. For example, in x + y[z] we want the access to have higher precedence than +, but in x[y + z] we want the opposite. An infix operator normally has the same precedence on both sides.

Another is that infix operators need to decide on associativity. Neither of x[y][z] or x[y[z]] is clearly a better choice in general for the meaning of x @ y @ z. Probably multidimensional array access x[y][z] is more commonly used, but left-associativity would mean the operator binds slightly looser on the left, contradicting the above. (That is, if @ is left-associative then x @ y binds where y is the right operand, instead of y @ z where y is the left operand.)

Both of these reasons can be summarised by saying that we would often need to use parentheses anyway to disambiguate usages of an infix array access operator; so we might as well make the parentheses part of the syntax, and then we don't need the additional symbol for an infix operator.

I think it's worth clarifying that array access is a binary operator, since it has two operands; it is (usually) not an infix operator, i.e. it is not a single token written between the operands.

There are a couple of arguments in favour of the x[y] syntax for arrays, over an infix operator.

One is that we want array access to bind the left operand more tightly than the right operand. For example, in x + y[z] we want the access to have higher precedence than +, but in x[y + z] we want the opposite. An infix operator normally has the same precedence on both sides.

Another is that infix operators need to decide on associativity. Neither of x[y][z] or x[y[z]] is clearly a better choice in general for the meaning of x @ y @ z. Probably multidimensional array access x[y][z] is more commonly used, but left-associativity would mean the operator binds slightly looser on the left, contradicting the above. (That is, if @ is left-associative then x @ y binds where y is the right operand, instead of y @ z where y is the left operand.)

Both of these reasons can be summarised by saying that we would often need to use parentheses anyway to disambiguate usages of an infix array access operator; so we might as well make the parentheses part of the syntax, and then we don't need the additional symbol for an infix operator.

There are a couple of arguments in favour of the x[y] syntax for arrays, over an infix operator.

One is that we want array access to bind the left operand more tightly than the right operand. For example, in x + y[z] we want the access to have higher precedence than +, but in x[y + z] we want the opposite. An infix operator normally has the same precedence on both sides.

Another is that infix operators need to decide on associativity. Neither of x[y][z] or x[y[z]] is clearly a better choice in general for the meaning of x @ y @ z. Probably multidimensional array access x[y][z] is more commonly used, but left-associativity would mean the operator binds slightly looser on the left, contradicting the above. (That is, if @ is left-associative then x @ y binds where y is the right operand, instead of y @ z where y is the left operand.)

Both of these reasons can be summarised by saying that we would often need to use parentheses anyway to disambiguate usages of an infix array access operator; so we might as well make the parentheses part of the syntax, and then we don't need the additional symbol for an infix operator.

added 320 characters in body
Source Link
kaya3
  • 22.4k
  • 50
  • 139

I think it's worth clarifying that array access is a binary operator, since it has two operands; it is (usually) not an infix operator, i.e. it is not a single token written between the operands.

There are a couple of arguments in favour of the x[y] syntax for arrays, over an infix operator.

One is that we want array access to bind the left operand more tightly than the right operand. For example, in x + y[z] we want the access to have higher precedence than +, but in x[y + z] we want the opposite. An infix operator normally has the same precedence on both sides.

Another is that infix operators need to decide on associativity. Neither of x[y][z] or x[y[z]] is clearly a better choice in general for the meaning of x @ y @ z. Probably multidimensional array access x[y][z] is more commonly used, but left-associativity would mean the operator binds slightly looser on the left, contradicting the above. (That is, if @ is left-associative then x @ y binds where y is the right operand, instead of y @ z where y is the left operand.)

Both of these reasons can be summarised by saying that we would often need to use parentheses anyway to disambiguate usages of an infix array access operator; so we might as well make the parentheses part of the syntax, and then we don't need the additional symbol for an infix operator.

I think it's worth clarifying that array access is a binary operator, since it has two operands; it is (usually) not an infix operator, i.e. it is not a single token written between the operands.

There are a couple of arguments in favour of the x[y] syntax for arrays, over an infix operator.

One is that we want array access to bind the left operand more tightly than the right operand. For example, in x + y[z] we want the access to have higher precedence than +, but in x[y + z] we want the opposite. An infix operator normally has the same precedence on both sides.

Another is that infix operators need to decide on associativity. Neither of x[y][z] or x[y[z]] is clearly a better choice in general for the meaning of x @ y @ z.

Both of these reasons can be summarised by saying that we would often need to use parentheses anyway to disambiguate usages of an infix array access operator; so we might as well make the parentheses part of the syntax, and then we don't need the additional symbol for an infix operator.

I think it's worth clarifying that array access is a binary operator, since it has two operands; it is (usually) not an infix operator, i.e. it is not a single token written between the operands.

There are a couple of arguments in favour of the x[y] syntax for arrays, over an infix operator.

One is that we want array access to bind the left operand more tightly than the right operand. For example, in x + y[z] we want the access to have higher precedence than +, but in x[y + z] we want the opposite. An infix operator normally has the same precedence on both sides.

Another is that infix operators need to decide on associativity. Neither of x[y][z] or x[y[z]] is clearly a better choice in general for the meaning of x @ y @ z. Probably multidimensional array access x[y][z] is more commonly used, but left-associativity would mean the operator binds slightly looser on the left, contradicting the above. (That is, if @ is left-associative then x @ y binds where y is the right operand, instead of y @ z where y is the left operand.)

Both of these reasons can be summarised by saying that we would often need to use parentheses anyway to disambiguate usages of an infix array access operator; so we might as well make the parentheses part of the syntax, and then we don't need the additional symbol for an infix operator.

added 201 characters in body
Source Link
kaya3
  • 22.4k
  • 50
  • 139
Loading
Source Link
kaya3
  • 22.4k
  • 50
  • 139
Loading