-
-
Notifications
You must be signed in to change notification settings - Fork 8
RFC 0018: Macro Methods #18
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
base: main
Are you sure you want to change the base?
Conversation
|
Some personal observations:
|
I actually wanted to post an RFC like this soon, only adding type restrictions and the ability to call macros weithin macros, but leaving the macro def syntax as a future consideration. I think I'll now wait how this RFC progresses. |
|
@BlobCodes that's an intriguing perspective. macro def format_name(name : StringLiteral) : StringLiteral
name.underscore.upcase
end
macro format_name(name : StringLiteral) : StringLiteral
{{ name.underscore.upcase }}
end |
macro generate_constant(name)
CONST_{{ name.id }}
end
|
There is no explicit return value, the entire macro is executed as it works right now, its entire output is parsed into an ASTNode and this ASTNode is then returned to the caller. If the given example macro was called with the name "foo", it would return Path("CONST_foo").
Yes, explicit returns would probably conflict with the current template syntax. That's something that could be explored with macro defs.
Why should a macro result in undefined behaviour if it's called within a nested context? |
I was mainly thinking about my use case where some of my macro foo(x)
{%
# Interacting with constants and stuff
# Intended to be called within a macro expression at certain places in the code
%}
endSo if you tried to use this macro outside of a macro expression where you're expected for it to run, what would it do? Probably error that the const is unknown? Maybe not a big deal in practice, but this is kinda why i liked |
Preview: https://github.com/crystal-lang/rfcs/blob/1195640eaf4f6780bb02e1839a35552adb42d04e/text/0018-macro-methods.md
PoC PR: TODO
A proposal based on the OP from crystal-lang/crystal#8835. Simple re-usable macro methods without the extra AST node monkey patching stuff.