-
-
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
Allow zero-method macros ? ? #22098
Comments
macro(s) are to work on some code and deliver the result to run-time . |
Creating a zero-method macro makes it available to metaprogramming, the module system and the documentation system. (I personally wanted this for docstring purposes) |
Can you give a concrete example of where you need this? |
i suggest
it define a macro |
Seems possible for the frontend to allow this. |
diff --git a/src/julia-parser.scm b/src/julia-parser.scm
index 7e05db3f7ff..ef8942bfd0a 100644
--- a/src/julia-parser.scm
+++ b/src/julia-parser.scm
@@ -1285,11 +1285,11 @@
((function macro)
(let* ((paren (eqv? (require-token s) #\())
(sig (parse-def s (not (eq? word 'macro)))))
- (if (and (eq? word 'function) (not paren) (symbol-or-interpolate? sig))
+ (if (and (not paren) (symbol-or-interpolate? sig))
(begin (if (not (eq? (require-token s) 'end))
- (error (string "expected \"end\" in definition of function \"" sig "\"")))
+ (error (string "expected \"end\" in definition of " word " \"" sig "\"")))
(take-token s)
- `(function ,sig))
+ `(,word ,sig))
(let* ((usig (unwrap-where sig))
(def (if (or (symbol? usig)
(and (pair? usig) (eq? (car usig) '|::|)
diff --git a/src/julia-syntax.scm b/src/julia-syntax.scm
index 78bdb2cd76d..cd289d4a2b0 100644
--- a/src/julia-syntax.scm
+++ b/src/julia-syntax.scm
@@ -1169,6 +1169,8 @@
v))
anames))
,@(cddr e)))))
+ ((and (length= e 2) (symbol? (cadr e)))
+ (expand-forms `(function ,(symbol (string #\@ (cadr e))))))
(else
(error "invalid macro definition")))) ? |
It seems worthwhile to support this, if only for the sake of consistency. We allow zero-argument functions, after all. |
@fcard that patch looks like a good start to me. I assume this will also need support in the docs system (to detect it as a macro declaration for your original purpose of using this for doc strings), and it'll need a test. |
FYI, the relevant line to change in the docsystem is this one. - isexpr(x, :function) && !isexpr(x.args[1], :call) ? objectdoc(source, mod, meta, def, x) :
+ isexpr(x, [:function, :macro]) && !isexpr(x.args[1], :call) ? objectdoc(source, mod, meta, def, x) : And I think that does it. Tests could be (in test/core.jl?) # issue 22098
macro m22098 end
@test_throws MethodError @eval @m22098() Mimicking the test for zero-method functions. (minus the and (test/docs.jl) # issue 22098
"an empty macro"
macro mdoc22098 end
@test docstrings_equal(@doc(:@mdoc22098), doc"an empty macro") |
When you have an issue description so unclear as the first post people might misunderstand you. When you said "I personally wanted this for docstring purposes" it looked like you wanted to know how to define a macro docstring without having to give the arguments to it which is what the linked documentation did. No one is trying to diminish your knowledge, just trying to be as helpful as they can. |
sorry |
Also, if you look at the second part of that document, it also clearly tell you the syntax for documenting an macro independent of it's definition, which afaict it what you claim you actually want. Zero argument macros are much less useful than functions since dispatch on them doesn't work the same (and dispatching is much less useful on macros). Having them just for some sort of consistency should be fine too though.... |
You can't document a macro if it doesn't exist. julia> "---" :@m
ERROR: UndefVarError: @m not defined Which is what I wanted. The only contributions I want to make are suggestions and code. I hate arguing. |
There's nothing wrong with allowing zero-method macros; I don't think anybody's seriously against them. @fcard Please feel free to make a PR with your changes. |
Agreed. I think this was a good suggestion. Did you mean to make the PR (fcard#10) against your own repo and slyly link it to the |
yes All code I posted here (and there) is licensed under the MIT license. (just in case) |
?
?
The text was updated successfully, but these errors were encountered: