Is it possible to use "←" for assignment?

Recently I was implementing some code following a paper that used the notation “←” for assignment, e.g.

a ← b + c

Since this is a common notation in computer science, it would be useful to be able to use this in Julia as an alias for “=”.

As far as I understand, this would require modifying the parser. If so, would this be acceptable?

3 Likes

Having used R before, I always this super confusing:

https://stat.ethz.ch/R-manual/R-devel/library/base/html/assignOps.html

I know it’s not the same proposal (you’re proposing an alias), but I am just scared of having more than 1 assignment operator after that experience. And it might trip up incoming R users who expect different behaviors out of the operators.

Why is it super confusing? TBH I’ve never noticed the difference between <- and = in R.

1 Like

What I really meant is that I would like to be able to assign as an alias for = in my own code, not really that it should be part of base Julia.

However, since “=” is not a normal Julia function, this does not currently seem to be possible, and that’s why it seems like the only option would be to change things at the level of the parser.

Oh I see. You be you :slight_smile:. Are macros not able to parse a ← b + c?

Yes, a macro would be possible, since it is valid Julia code:

julia v0.5> parse("a ← b + c")
:(a ← b + c)

julia v0.5> Meta.show_sexpr(ans)
(:call, :←, :a, (:call, :+, :b, :c))

Possible, but clunky…

If you use something like MacroTalks.prewalk, it’s pretty straightforward to write a macro that can go in front of a whole block of code and transform all instances of .

2 Likes

Note that this would be a breaking change, as there is code that uses for a different purpose.