You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've always found the documentation of operators a little disjointed and
incomplete; this PR aims to improve the situation.
- Add missing items to the precedence table
- Adjoint operator goes after `::` and before exponentiation
- Juxtaposition of numeric literals goes after unary operators and
before bitshifts
- Arrows go between `||` and `?`
- New footnote explains exceptions to exponentiation precedence
- New footnote explains non-associativity of comparisons, but notes
chaining
- Add more complete lists of operators
- Include *all* operators defined in Base
- Include operators that are available but not defined in Base, set off
by parentheses
- Operator suffixes
- Note that new operators can be defined with suffixes
- Explain what those prefixes are
- Show an example
- Provide some links between disjoint pages
- Link from [Manual >
Variables](https://docs.julialang.org/en/v1/manual/variables/#man-allowed-variable-names)
to operator section
- Link from [Manual > Integers and Floating-Point
Numbers](https://docs.julialang.org/en/v1/manual/integers-and-floating-point-numbers/#Integers-and-Floating-Point-Numbers)
to operator section
- Link from [Manual >
Functions](https://docs.julialang.org/en/v1/manual/functions/#man-functions)
to operator section
- Link back to functions page when noting that operators are functions.
---------
Co-authored-by: Steven G. Johnson <stevenj@mit.edu>
Co-authored-by: Max Horn <max@quendi.de>
The unary operators `+` and `-` require explicit parentheses around their argument to disambiguate them from the operator `++`, etc. Other compositions of unary operators are parsed with right-associativity, e. g., `√√-a` as `√(√(-a))`.
422
463
[^2]:
423
-
The operators `+`, `++` and `*` are non-associative. `a + b + c` is parsed as `+(a, b, c)` not `+(+(a, b),
424
-
c)`. However, the fallback methods for `+(a, b, c, d...)` and `*(a, b, c, d...)` both default to left-associative evaluation.
425
-
426
-
For a complete list of *every* Julia operator's precedence, see the top of this file:
427
-
[`src/julia-parser.scm`](https://github.com/JuliaLang/julia/blob/master/src/julia-parser.scm). Note that some of the operators there are not defined
428
-
in the `Base` module but may be given definitions by standard libraries, packages or user code.
429
-
430
-
You can also find the numerical precedence for any given operator via the built-in function `Base.operator_precedence`, where higher numbers take precedence:
464
+
Unary operators and juxtaposition of numeric literals take precedence over `^` only *within the exponent*. For example, `2^-3`, `x^√2`, and `2^3x` are parsed as `2^(-3)`, `x^(√2)`, and `2^(3*x)`; whereas `-2^3`, `√x^2`, `2^3*x`, and `2x^3` are parsed as `-(2^3)`, `√(x^2)`, `(2^3)*x`, and `2*(x^3)`.
465
+
[^3]:
466
+
Note that most unary operators can be composed, except `++` which is a distinct *binary* operator, and `--` which produces a `ParseError`. Other compositions of unary operators are parsed with right-associativity — e.g., `√√-a` as `√(√(-a))`.
467
+
[^4]:
468
+
The operators `+`, `++` and `*` are parsed differently. For example, `a + b + c` is parsed as `+(a, b, c)` not `+(+(a, b),c)`. However, the fallback methods for `+(a, b, c, d...)` and `*(a, b, c, d...)` both default to left-associative evaluation. Note that `++` is not defined in `Base`, but is parsed in the same way.
469
+
[^5]:
470
+
Comparisons can be [chained](@ref"Chaining comparisons"). For example, `a < b < c` is essentially the same as `a < b && b < c`. However, the order of evaluation is undefined.
471
+
472
+
It is also possible to define additional operators by appending suffixes to most of the binary operators. The valid
473
+
suffixes include the Unicode combining characters, along with the subscripts, superscripts, and various primes
474
+
(`′ ″ ‴ ⁗ ‵ ‶ ‷`) listed in
475
+
[`src/flisp/julia_opsuffs.h`](https://github.com/JuliaLang/julia/blob/master/src/flisp/julia_opsuffs.h). The
476
+
resulting operators can be used with either functional or infix notation, and have the same precedence and
477
+
associativity as the base operator. For example, `⋆̂ᵝ₁′` could be defined as a function, and used as an infix operator
478
+
with the same precedence and associativity as `⋆` and `*`. However, operators ending with a subscript or superscript
479
+
letter must be followed by a space when used in infix notation to distinguish them from variable names that begin
480
+
with a subscript or superscript letter. For example, if `+ᵃ` is an operator, then `+ᵃx` must be written as `+ᵃ x`
481
+
to distinguish it from `+ ᵃx`.
482
+
483
+
You can also find the numerical precedence for any binary or ternary operator via the
484
+
built-in function `Base.operator_precedence`, where higher numbers take precedence:
@@ -449,6 +503,8 @@ julia> Base.operator_associativity(:⊗), Base.operator_associativity(:sin), Bas
449
503
450
504
Note that symbols such as `:sin` return precedence `0`. This value represents invalid operators and not
451
505
operators of lowest precedence. Similarly, such operators are assigned associativity `:none`.
506
+
Some valid operators, such as `+`, `++`, `*`, and the comparison operators, also report associativity
507
+
`:none` because they are neither left- nor right-associative.
452
508
453
509
[Numeric literal coefficients](@ref man-numeric-literal-coefficients), e.g. `2x`, are treated as multiplications with higher precedence than any other binary operation, with the exception of `^` where they have higher precedence only as the exponent.
Copy file name to clipboardExpand all lines: doc/src/manual/variables.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -108,7 +108,8 @@ digits (0-9 and other characters in categories Nd/No), as well as other Unicode
108
108
and other modifying marks (categories Mn/Mc/Me/Sk), some punctuation connectors (category Pc),
109
109
primes, and a few other characters.
110
110
111
-
Operators like `+` are also valid identifiers, but are parsed specially. In some contexts, operators
111
+
[Operators](@ref Operator-Precedence-and-Associativity) like `+` are also valid identifiers, but
112
+
are parsed specially. In some contexts, operators
112
113
can be used just like variables; for example `(+)` refers to the addition function, and `(+) = f`
113
114
will reassign it. Most of the Unicode infix operators (in category Sm), such as `⊕`, are parsed
114
115
as infix operators and are available for user-defined methods (e.g. you can use `const ⊗ = kron`
@@ -118,7 +119,6 @@ A space is required between an operator that ends with a subscript/superscript l
118
119
variable name. For example, if `+ᵃ` is an operator, then `+ᵃx` must be written as `+ᵃ x` to distinguish
119
120
it from `+ ᵃx` where `ᵃx` is the variable name.
120
121
121
-
122
122
A particular class of variable names is one that contains only underscores. These identifiers are write-only. I.e. they can only be assigned values, which are immediately discarded, and their values cannot be used in any way.
0 commit comments