Skip to content
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

Closed
fcard opened this issue May 27, 2017 · 17 comments
Closed

Allow zero-method macros ? ? #22098

fcard opened this issue May 27, 2017 · 17 comments
Labels
compiler:lowering Syntax lowering (compiler front end, 2nd stage) docsystem The documentation building system macros @macros parser Language parsing and surface syntax status:help wanted Indicates that a maintainer wants help on an issue or pull request

Comments

@fcard
Copy link
Contributor

fcard commented May 27, 2017

?

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

?

@DrKrar
Copy link
Contributor

DrKrar commented May 27, 2017

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.

@fcard
Copy link
Contributor Author

fcard commented May 27, 2017

Creating a zero-method macro makes it available to metaprogramming, the module system and the documentation system. (I personally wanted this for docstring purposes)

@nalimilan
Copy link
Member

Can you give a concrete example of where you need this?

@DrKrar
Copy link
Contributor

DrKrar commented May 28, 2017

i suggest

"docstring"    
macro m() end

it define a macro

@fredrikekre
Copy link
Member

https://docs.julialang.org/en/stable/manual/documentation/#macros

@vtjnash vtjnash added parser Language parsing and surface syntax status:help wanted Indicates that a maintainer wants help on an issue or pull request labels May 28, 2017
@vtjnash
Copy link
Sponsor Member

vtjnash commented May 28, 2017

Seems possible for the frontend to allow this.

@fcard
Copy link
Contributor Author

fcard commented May 28, 2017

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.

@stevengj
Copy link
Member

It seems worthwhile to support this, if only for the sake of consistency. We allow zero-argument functions, after all.

@vtjnash
Copy link
Sponsor Member

vtjnash commented May 30, 2017

@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.

@JeffBezanson JeffBezanson added docsystem The documentation building system compiler:lowering Syntax lowering (compiler front end, 2nd stage) labels Jun 1, 2017
@fcard
Copy link
Contributor Author

fcard commented Jun 6, 2017

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

@KristofferC
Copy link
Sponsor Member

KristofferC commented Jun 6, 2017

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.

@fcard
Copy link
Contributor Author

fcard commented Jun 6, 2017

sorry

@yuyichao
Copy link
Contributor

yuyichao commented Jun 6, 2017

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....

@fcard
Copy link
Contributor Author

fcard commented Jun 6, 2017

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.

@JeffBezanson
Copy link
Sponsor Member

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.

@vtjnash
Copy link
Sponsor Member

vtjnash commented Jun 6, 2017

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 ? above?

@fcard
Copy link
Contributor Author

fcard commented Jun 7, 2017

yes

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler:lowering Syntax lowering (compiler front end, 2nd stage) docsystem The documentation building system macros @macros parser Language parsing and surface syntax status:help wanted Indicates that a maintainer wants help on an issue or pull request
Projects
None yet
Development

No branches or pull requests

9 participants