Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

document macro hygienification; expose it to the user #1191

Closed
o-jasper opened this issue Aug 20, 2012 · 7 comments
Closed

document macro hygienification; expose it to the user #1191

o-jasper opened this issue Aug 20, 2012 · 7 comments
Assignees
Labels
docs This change adds or pertains to documentation

Comments

@o-jasper
Copy link

The transformation that quoted code generated by macros undergoes before being inserted into a local AST needs two things:

  1. documentation in metaprogramming
  2. exposure to the user

These are related because documentation will be much easier and more effective if it can be demonstrated.

ORIGINAL ISSUE

On e519c91, the following:

function value_one() #Of course it works
   1
end
macro defv2 ()                      
   :(function value_two()
      2
     end)
end
@defv2
value_two #Not defined!

Found this out with the get_c_fun macro.. I'd much prefer to keep using that.

@StefanKarpinski
Copy link
Member

This is a macro hygiene issue. Since modules were introduced, there are now two contexts that each macro needs to work with: the defining context and the evaluating context. The rules are basically that things that are localish — meaning things that in a scope would be made local via assignment or whatever — are automatically renamed not to clash with the calling context (this avoids the need for gensyms most of the time); things that are globalish are assumed to come from the macro definition context and are rewritten as such. In order for something to not be interpreted as in the definition context or as something the be rewritten as a local, it has to be escaped using the esc function. In this case, value_two is rewritten to a non-clashing name as though it were gensym:d. This version does what you want:

julia> macro defv2 ()
          :(function ($esc(:value_two))()
             2
            end)
       end

julia> @defv2

julia> value_two
Methods for generic function value_two
value_two() at none:3

julia> value_two()
2

@toivoh
Copy link
Contributor

toivoh commented Aug 21, 2012

So the issue is really to update the julia manual?
I guess a newsletter could be a good place to announce these kind of changes too.

@o-jasper
Copy link
Author

If I println the value you don't see value_two replaced by gensyms. Looks like the macro output is hygienized afterward, so i don't really understand how this relates to $ notation, you can get the same avoiding $ completely;

macro defv2 ()
  Expr(:function,{Expr(:call,{:value_two},Any),2},Any)
end

If it is just a function applied afterward, maybe it should be exposed to the user? Then they can esc something and hygienize some subtrees of it.(I can't find an unesc or `hygienize function so far) You could ofcourse leave it up to the user the hygienize, but maybe hygienic macros might get underused that way..

If you do apply hygienize afterward in regular macros, it is probably a good idea to give the user the tools to look at the result. Something like a macro_expand_1. (A macro version could help a bit with the workflow, as then you don't need to quote it.)

@StefanKarpinski
Copy link
Member

If it is just a function applied afterward, maybe it should be exposed to the user? Then they can esc something and hygienize some subtrees of it.(I can't find an unesc or `hygienize function so far) You could ofcourse leave it up to the user the hygienize, but maybe hygienic macros might get underused that way.

100% agree. In fact, I'm turning this issue into a request for this to be documented in the metaprogramming section and to expose this functionality to the user.

@ghost ghost assigned JeffBezanson Aug 21, 2012
JeffBezanson added a commit that referenced this issue Aug 22, 2012
@JeffBezanson
Copy link
Member

Which copy of the manual do I edit? <-- major problem

@StefanKarpinski
Copy link
Member

What do you mean? There's only one at this point.

@JeffBezanson
Copy link
Member

Done.

cmcaine pushed a commit to cmcaine/julia that referenced this issue Nov 11, 2022
KristofferC added a commit that referenced this issue Feb 25, 2025
)

Stdlib: LinearAlgebra
URL: https://github.com/JuliaLang/LinearAlgebra.jl.git
Stdlib branch: master
Julia branch: master
Old commit: e7da19f
New commit: f781708
Julia version: 1.13.0-DEV
LinearAlgebra version: 1.12.0(Does not match)
Bump invoked by: @KristofferC
Powered by:
[BumpStdlibs.jl](https://github.com/JuliaLang/BumpStdlibs.jl)

Diff:
JuliaLang/LinearAlgebra.jl@e7da19f...f781708

```
$ git log --oneline e7da19f..f781708
f781708 use the custom sysimage when running documentation (and doctests) as well (#1226)
8fdbfd5 Add `getindex` for `SymTridiagonal` using a `BandIndex` (#1223)
91b8845 Use `BandIndex` directly in `diagzero` call in `getindex` (#1222)
ef7ef3a Restrict triangular type aliases to `AbstractMatrix`es (#1220)
af7a9ac Use `BandIndex` directly in `diagzero` call in `getindex` for banded matrices
579b5f7 Specialize Diagonal * Adjoint (#1207)
5cf41c4 Indirection in matrix multiplication to avoid ambiguities (#1210)
0a9c164 Remove specialized `issymmetric`/`ishermitian` for `Diagonal{<:Number}` (#1213)
ff5648a Make unitary matrices in `svd`/`eigen` of `Diagonal` be unitless (#1155)
e096a03 Don't mutate arrays in symmetric trig functions (#1206)
c234bed Loop over `diagind` in `diag` for banded matrices (#1211)
57785c7 More resizing for truncating return values from LAPACK (#1190)
b464203 Materialize adjoint in mul with `HermOrSym` (#1191)
16d9d61 Restrict Diagonal sqrt branch to positive diag (#1203)
baa48b7 Verbose `showerror` for `SingularException` (#1204)
e0b59a7 Remove `LinearAlgebra` qualifications in `cholesky.jl` (#1209)
95d009b Remove `LinearAlgebra` qualifications in `cholesky.jl`
c550974 change to pivot
ed35a37 Add fast path in generic matmul (#1202)
8c7fe68 Detailed `showerror` for `SingularException`
2a1696a Explicitly declare type constructor imports (#1196)
101f766 Added note to BLAS.[g|s]et_num_threads about Apple Accelerate not supporting it  (#1195)
5aca26f Simplify `getproperty` for `Cholesky*` (#1197)
924dda4 remove copy-allocation on accessing `cholesky` factors (`.L`, `.U`) (#1186)
6f02532 Use `BLAS.trsm!` instead of `LAPACK.trtrs!` in left-triangular solves (#1194)
```

Co-authored-by: KristofferC <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This change adds or pertains to documentation
Projects
None yet
Development

No branches or pull requests

4 participants