MacroTools.jl icon indicating copy to clipboard operation
MacroTools.jl copied to clipboard

`isdef`with `quote` seems giving incorrect result

Open mmikhasenko opened this issue 1 year ago • 3 comments

I need to convert a mathematical function in a string into Julia lambda function. Just found the package, it seems very useful.

Just stumbled by a very basic example: expr = :(x -> x + 2) is recognized as a function by isdef, while quote x -> x + 2 end is not recognized.

MWE

julia> expr = quote
    x -> x + 2
end

julia> MacroTools.isdef(expr)
false

julia> eval(expr)(3)
5

julia v1.10 MacroTools v0.5.13

mmikhasenko avatar May 31 '24 14:05 mmikhasenko

Ah, that's slightly awkward. On one hand, we could do striplines here (and/or remove the begin block) and it would work fine. On the other hand, expr is technically a block that contains a function definition; not a function definition.

I would lean towards lining up the behaviour with splitdef. Does splitdef(expr) work?

cstjean avatar Jun 03 '24 01:06 cstjean

yes, splitdef works. It correctly identifies the body and args

mmikhasenko avatar Jun 03 '24 08:06 mmikhasenko

Then let's fix isdef! Thank you for the bug report. I think it's basically just to remove the block. There's already a function for it in utils.jl IIRC

cstjean avatar Jun 03 '24 21:06 cstjean