It can be helpful to look at the sexpr form:

julia> Meta.show_sexpr(expand(parse(“x[:] += 1”)))
(:body, (:(=), GenSym(0), (:call, :colon, 1, (:call, :(top(endof)), :x))),
(:(=), GenSym(1), (:call, :+, (:call, :getindex, :x, GenSym(0)), 1)),
(:call, :setindex!, :x, GenSym(1), GenSym(0)), (:return, GenSym(1)))

A number of these elements are compiler-generated and not useable directly
(because they aren’t needed). top(...) is special-syntax needed during
bootstrapping the sysimg, but is generally just equivalent to the ... part.
GenSym is an unnamed, auto-generated variable, and can be replaced by a
normal, named variable.

It’s a pretty simple transform to do manually, replacing end with a call to
endof, : with colon, and [] with either getindex or setindex:

irange = colon(1,endof(x))

setindex!(x, getindex(x, irange) + 1,irange)

On Sat, Mar 14, 2015 at 1:18 PM Tamas Papp [email protected]
<http://mailto:[email protected]> wrote:

Thanks. Is the (top(endof)) syntax something that is exposed to the
> user? Did not work for me:
>
> julia> colon(1,(top(endof))([1:4;]))
> ERROR: UndefVarError: top not defined
>
> Would be nice when I need to construct the range generated by a lone :
> in a macro.
>
> Best,
>
> Tamas
>
> On Sat, Mar 14 2015, Matt Bauman wrote:
>
> > On Saturday, March 14, 2015 at 9:15:32 AM UTC-4, Tamas Papp wrote:
> >
> >     I don't know if the last step is accessible to the user (for
> >     metaprogramming), would be interesting to learn about that.
> >
> >
> > Here you go:
> >
> > julia> expand(parse("x[:] += 1"))
> > :(begin
> > GenSym(0) = colon(1,(top(endof))(x))
> > GenSym(1) = getindex(x,GenSym(0)) + 1
> > setindex!(x,GenSym(1),GenSym(0))
> > return GenSym(1)
> > end)
>
​

Reply via email to