Julia: Allow zero-method macros ? ?

Created on 27 May 2017  路  17Comments  路  Source: JuliaLang/julia

?

julia> macro m end
ERROR: syntax: expected "(" in macro definition

?

docsystem help wanted lowering macros parser

Most helpful comment

All 17 comments

macro(s) are to work on some code and deliver the result to run-time .
what is the use of a macro having no thing to work on.

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

"docstring"    
macro m() end

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"))))

?
Also count me out of the discussion.

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.
Tragically that messes with the whitespace alignment.

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 isa Function test because I don't see any documentation or test explicitly stating that macro m end will always result in a@m function)

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.
Either way, I don't want to be part of this conversation.

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 (https://github.com/fcard/julia/pull/10) against your own repo and slyly link it to the ? above?

yes

All code I posted here (and there) is licensed under the MIT license. (just in case)

Was this page helpful?
0 / 5 - 0 ratings