Reputation: 505
Like in R:
a <- 2
or even better
a ← 2
which should translate to
a = 2
and if possible respect method overloading.
Upvotes: 2
Views: 586
Reputation: 2260
Disclaimer: You are fully responsible if you will try my (still beginner's) experiments bellow! :P
MacroHelper is module ( big thanks to @Alexander_Morley and @DanGetz for help ) I plan to play with in future and we could probably try it here :
julia> module MacroHelper
# modified from the julia source ./test/parse.jl
function parseall(str)
pos = start(str)
exs = []
while !done(str, pos)
ex, pos = parse(str, pos) # returns next starting point as well as expr
ex.head == :toplevel ? append!(exs, ex.args) : push!(exs, ex)
end
if length(exs) == 0
throw(ParseError("end of input"))
elseif length(exs) == 1
return exs[1]
else
return Expr(:block, exs...) # convert the array of expressions
# back to a single expression
end
end
end;
With module above you could define simple test "language":
julia> module TstLang
export @tst_cmd
import MacroHelper
macro tst_cmd(a)
b = replace("$a", "←", "=") # just simply replacing ←
# in real life you would probably like
# to escape comments, strings etc
return MacroHelper.parseall(b)
end
end;
And by using it you could probably get what you want:
julia> using TstLang
julia> tst```
a ← 3
println(a)
a +← a + 3 # maybe not wanted? :P
```
3
9
What about performance?
julia> function test1()
a = 3
a += a + 3
end;
julia> function test2()
tst```
a ← 3
a +← a + 3
```
end;
julia> test1(); @time test1();
0.000002 seconds (4 allocations: 160 bytes)
julia> test2(); @time test2();
0.000002 seconds (4 allocations: 160 bytes)
If you like to see syntax highlight (for example in atom editor) then you need to use it differently:
function test3()
@tst_cmd begin
a ← 3
a ← a + a + 3 # parser don't allow you to use "+←" here!
end
end;
We could hope that future Julia IDEs could syntax highlight cmd macros too. :)
What could be problem with "solution" above? I am not so experienced julian so many things. :P (in this moment something about "macro hygiene" and "global scope" comes to mind...)
But what you want is IMHO good for some domain specific languages and not to redefine basic of language! It is because readability very counts and if everybody will redefine everything then it will end in Tower of Babel...
Upvotes: 0
Reputation: 106
=
is overloaded (not in the multiple dispatch sense) a lot in Julia.
It binds a new variable. As in a = 3
. You won't be able to use ←
instead of =
in this context, because you can't overload binding in Julia.
It gets lowered to setindex!
. As in, a[i] = b
gets lowered to setindex!(a, b, i)
. Unfortunately, setindex!
takes 3 variables while ←
can only take 2 variables. So you can't overload =
with 3 variables.
But, you can use only 2 variables and overload a[:] = b
, for example. So, you can define ←(a,b) = (a[:] = b)
or ←(a,b) = setindex!(a,b,:)
.
a .= b
gets lowered to (Base.broadcast!)(Base.identity, a, b)
. You can overload this by defining ←(a,b) = (a .= b)
or ←(a,b) = (Base.broadcast!)(Base.identity, a, b)
.
So, there are two potentially nice ways of using ←
. Good luck ;)
Btw, if you really want to use ←
to do binding (like in 1.), the only way to do it is using macros. But then, you will have to write a macro in front of every single assignment, which doesn't look very good.
Also, if you want to explore how operators get lowered in Julia, do f(a,b) = (a .= b)
, for example, and then @code_lowered f(x,y)
.
Upvotes: 5
Reputation: 8044
No. =
is not an operator in Julia, and cannot be assigned to another symbol.
Upvotes: 5