-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
"fake" multiple dispatch for different expression types in macros #20929
Comments
I'm pretty sure I remember this being brought up before and people thought it was a good idea. I also know there's been talk of "finalizing" our AST/Expr representation for 1.0 as well. |
This would largely rely on changing the AST representation to a more type-based one, where instead of using |
Going further, there could even be fully typed trees, like: ForExpr.iterator |
I would be very worried that that sort of thing would make symbolic manipulation too complicated. You'd have to remember or look up the fields for every possible type of expression. I think using |
I can see how it might be an issue to iterate. Maybe a dict? IfExpr.args[:condition] for example |
This is the kind of thing pattern matching is good for. |
What I mean is that while dispatching on expression heads is somewhat useful, you need to be able to match on full tree structures to get any real benefit. Expression structures are also very flexible and dynamic, so they are not a great fit for the design assumptions of our multiple dispatch. This kind of programming really demands a feature that converts a series of tree patterns to a nest of |
I've found #12102 which seems to be a suggestion that pattern matching for expressions be included in Base. The result seems to be that the poster wrote the MacroTools package which seems to still be actively maintained. I find it interesting that most of the discussion in that post is about users not necessarily being knowledgeable about the AST. Personally, I find that to be a very small issue, I simply use the REPL to check what the |
It seems that this has now effectively been implemented through Maybe I'm crazy but I could have sworn that at some point h(::Union{Type{Val{:call}},Type{Val{:(=)}}}, expr) = f(expr)
h(::Type{Val{:block}}, expr) = g(expr)
macro m(expr)
h(expr.head, expr)
end It's not beautiful, but it's certainly an improvement. Is there some simple way of using |
Use a real pattern-matching library (like the previously mentioned MacroTools package)? |
It would be really cool if one could do something like
which would be equivalent to
(of course we should think of a better name than
ExprHeadType
). Of course, this would have to then require that theexpr
argument be anExpr
rather than aSymbol
or literal. Any macros that aren't written withExprHeadType
would behave normally.Additionally, it would be nice to be able to dispatch functions on expression types (mainly for the purpose of helping macros), sort of like it's currently possible to do with specific values using
Val
. (I don't think that's a crazy extrapolation from current behavior, but I know very little about the compiler.)Is this completely crazy from a compiler perspective, or something people might be interested in?
The text was updated successfully, but these errors were encountered: