Skip to content

Use @& macro for borrowing #17

@MilesCranmer

Description

@MilesCranmer

So apparently you can actually get the @& macro working. Now, Julia prevents you from doing this normally:

julia> macro &(blah)
           esc(blah)
       end
ERROR: ParseError:
# Error @ REPL[2]:1:7
macro &(blah)
#     └─────┘ ── Invalid signature in macro definition

julia> macro :&(blah)
           esc(blah)
       end
ERROR: syntax: invalid macro definition around REPL[3]:1

but inside an @eval, it's totally fine:

julia> let x = :&
           @eval macro $(x)(blah)
               return esc(blah)
           end
       end
@& (macro with 1 method)

We could then literally have syntax like this:

@let x = @& y

for creating a reference to y. Perhaps these @let and @& macros could automatically adapt to the context...

function foo(x::@& Vector{Int}, y)
    sum(x) + y
end

this would transform into OrBorrowed{Vector{Int}}.

@bc foo(@&(x), @& :mut y)

this would be equivalent to @bc foo(x, @mut(y)) in the current notation.

@let x = [1, 2, 3]  #= owned variable =#
@let :mut y = [4, 5]  #= mutable owned variable =#
@let z = @& y  #= immutable reference to y =#

Only question is how to incorporate lifetimes here. I suppose ~lt just like the current @ref notation could be best?

Note that GitHub doesn't, but the Julia REPL renders these correctly:

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions