Reputation: 7674
The RecipesBase.jl @recipe
macro makes use of a couple special operators constructed out of ASCII characters, namely -->
and :=
. These character sequences seem to have some special attribute that allows them to be parsed into an Expr
. Compare -->
to --
:
julia> 1 --> 2
ERROR: syntax: invalid syntax 1 --> 2
julia> 1 -- 2
ERROR: syntax: invalid operator "--"
julia> :(1 --> 2)
:($(Expr(:-->, 1, 2)))
julia> :(1 -- 2)
ERROR: syntax: invalid operator "--"
Interestingly, 1 --> 2
is parsed with an expression head of :-->
, whereas other binary operators, including Unicode binary operators such as ↑
(typed as \uparrow
+ TAB
), are parsed with an expression head of :call
:
julia> dump(:(1 --> 2))
Expr
head: Symbol -->
args: Array{Any}((2,))
1: Int64 1
2: Int64 2
julia> dump(:(1 ↑ 2))
Expr
head: Symbol call
args: Array{Any}((3,))
1: Symbol ↑
2: Int64 1
3: Int64 2
So, I have a few related questions:
-->
and :=
? (EDIT: In other words, why are those character sequences specially parsed?)-->
and :=
and that can therefore be used as operators in macros?Upvotes: 7
Views: 142
Reputation: 1742
-->
and :=
are specially parsed by the Julia parser.
Take a look at this file: https://github.com/JuliaLang/julia/blob/f54cdf45a9e04f1450ba22142ddac8234389fe05/src/julia-parser.scm
It lists all of the specially parsed character sequences, and I'm pretty sure you can also get the associativity from it.
Upvotes: 4