Multi-line `@enum`

226 views
Skip to first unread message

Scott Jones

unread,
Apr 27, 2015, 9:14:11 AM4/27/15
to juli...@googlegroups.com
I can't find anything in the documentation for 0.4, or since enums were added in julia-dev or julia-users, about how to make an Enum that is multiple lines... what am I missing?
Thanks, Scott

Tamas Papp

unread,
Apr 27, 2015, 9:29:50 AM4/27/15
to juli...@googlegroups.com
You should be able to call @enum with ()'s like any other macro, and then
include linebreaks. Eg

@enum(FRUIT, apple=1,
orange=2, kiwi=3)

Best,

Tamas

Mauro

unread,
Apr 27, 2015, 9:31:49 AM4/27/15
to juli...@googlegroups.com
I think you have to use the bracketed version of the macro call:

julia> @enum(FRUIT,apple=1,orange=2,
kiwi=3)

(BTW this should work for any macro)

Scott Jones

unread,
Apr 27, 2015, 9:32:09 AM4/27/15
to juli...@googlegroups.com
Duh!  My newbieness strikes again!  I haven't really used macros yet... just one-line things like @code_native and @which...

Scott Jones

unread,
Apr 29, 2015, 6:01:23 PM4/29/15
to juli...@googlegroups.com
I must say... why are there *two* ways of writing a macro in Julia?
@enum(FRUIT,apple=1,orange=2,kiwi=3)
vs
@enum FRUIT apple=1 orange=2 kiwi=3
It seems that all you get from that is saving 1 character of typing, but you lose quite a bit,
like the ability to use juxtaposition for string (or even just string literal, a la C/C++) concatenation...,
and general readability of code...

On Monday, April 27, 2015 at 9:29:50 AM UTC-4, Tamas Papp wrote:

Stefan Karpinski

unread,
Apr 29, 2015, 6:11:15 PM4/29/15
to juli...@googlegroups.com
The space-separate, keyword-like one is standard, but there are places where the function-call form is more convenient.

Scott Jones

unread,
Apr 29, 2015, 10:05:16 PM4/29/15
to juli...@googlegroups.com
Is there anything that can be done with the “whitespace significant” form of macros, that cannot be done with a normal argument list?

If not, what’s the point?

Scott

ele...@gmail.com

unread,
Apr 29, 2015, 10:57:33 PM4/29/15
to juli...@googlegroups.com


On Thursday, April 30, 2015 at 12:05:16 PM UTC+10, Scott Jones wrote:
Is there anything that can be done with the “whitespace significant” form of macros, that cannot be done with a normal argument list?

@parallel for i=1:100000

Matt Bauman

unread,
Apr 29, 2015, 10:59:19 PM4/29/15
to juli...@googlegroups.com
Yes, the white space syntax is used all over the place: @inline, @inbounds, @eval, @simd, @test, not to mention the DSL-like macro usage in lots of packages.

Scott Jones

unread,
Apr 30, 2015, 8:17:13 AM4/30/15
to juli...@googlegroups.com
OK, I hadn't yet used any of those macros...  and haven't written anything other than the simplest macro myself yet.
What determines the end of the argument(s) for the form with just whitespace syntax?

(As somebody who also has written a number of parsers / compilers, the idea of significant whitespace drives me a bit to distraction...)

Thanks, Scott

Mauro

unread,
Apr 30, 2015, 8:40:18 AM4/30/15
to juli...@googlegroups.com
> OK, I hadn't yet used any of those macros... and haven't written anything
> other than the simplest macro myself yet.
> What determines the end of the argument(s) for the form with just
> whitespace syntax?

Newline or ;. I think the white-space rule in macro invocation is
simple: any space is treated as separator. There are other places in
julia where white-space is much more fiddly.

Mauro

unread,
Apr 30, 2015, 9:09:53 AM4/30/15
to juli...@googlegroups.com
On Thu, 2015-04-30 at 14:38, Mauro <maur...@runbox.com> wrote:
>> OK, I hadn't yet used any of those macros... and haven't written anything
>> other than the simplest macro myself yet.
>> What determines the end of the argument(s) for the form with just
>> whitespace syntax?
>
> Newline or ;. I think the white-space rule in macro invocation is
> simple: any space is treated as separator.

Well, newline after the end of a fully parsed Julia expression. So this
works:

julia> macro m(args...)
println(args)
end

julia> @m function f()
a = 1
end 5

(:(function f() # none, line 2:
a = 1
end),5)


> There are other places in julia where white-space is much more fiddly.

(At least from a users perspective)

Anyway, I think it works well, so no need for you to worry. I really
like the @ syntax as it clearly alerts the reader that now something
special is going on.

ele...@gmail.com

unread,
Apr 30, 2015, 9:17:33 AM4/30/15
to juli...@googlegroups.com


On Thursday, April 30, 2015 at 10:40:18 PM UTC+10, Mauro wrote:
> OK, I hadn't yet used any of those macros...  and haven't written anything
> other than the simplest macro myself yet.
> What determines the end of the argument(s) for the form with just
> whitespace syntax?

Newline or ;.  I think the white-space rule in macro invocation is
simple: any space is treated as separator.  There are other places in
julia where white-space is much more fiddly.

I think it continues past the newline if the final Expr continues past it, for example @parallel gets the whole `for` Expr as an argument no matter how many lines it is.
 

> (As somebody who also has written a number of parsers / compilers, the idea
> of significant whitespace drives me a bit to distraction...)

+1, but in this case the whitespace is not really significant, its just a separator, which is its usual purpose

Scott Jones

unread,
Apr 30, 2015, 9:25:19 AM4/30/15
to juli...@googlegroups.com
On Apr 30, 2015, at 9:17 AM, ele...@gmail.com wrote:



On Thursday, April 30, 2015 at 10:40:18 PM UTC+10, Mauro wrote:
> OK, I hadn't yet used any of those macros...  and haven't written anything
> other than the simplest macro myself yet.
> What determines the end of the argument(s) for the form with just
> whitespace syntax?

Newline or ;.  I think the white-space rule in macro invocation is
simple: any space is treated as separator.  There are other places in
julia where white-space is much more fiddly.

I think it continues past the newline if the final Expr continues past it, for example @parallel gets the whole `for` Expr as an argument no matter how many lines it is.
 

> (As somebody who also has written a number of parsers / compilers, the idea
> of significant whitespace drives me a bit to distraction...)

+1, but in this case the whitespace is not really significant, its just a separator, which is its usual purpose

Having to use it as a separator *is* significant, and is why I don’t think you can have both juxtaposition for string concatenation at the same time as significant whitespace for macros…

i.e.
mymacro(a,b) ; println(“<$a><$b>”) ; end

myVar = “foo” “bar” # if juxtaposition did string concatenation
->”foobar”

@mymacro “foo” “bar” # just what does this mean now?

I also just noticed that juxtaposition for multiply only works if there are no spaces… i.e. 2n but not 2 n.

Scott

Scott Jones

unread,
Apr 30, 2015, 11:46:47 AM4/30/15
to juli...@googlegroups.com


On Thursday, April 30, 2015 at 9:09:53 AM UTC-4, Mauro wrote:
On Thu, 2015-04-30 at 14:38, Mauro <maur...@runbox.com> wrote:
>> OK, I hadn't yet used any of those macros...  and haven't written anything
>> other than the simplest macro myself yet.
>> What determines the end of the argument(s) for the form with just
>> whitespace syntax?
>
> Newline or ;.  I think the white-space rule in macro invocation is
> simple: any space is treated as separator.

Well, newline after the end of a fully parsed Julia expression.  So this
works:

julia> macro m(args...)
       println(args)
       end

julia> @m function f()
       a = 1
       end 5

(:(function f() # none, line 2:
        a = 1
    end),5)


> There are other places in julia where white-space is much more fiddly.

(At least from a users perspective)

Anyway, I think it works well, so no need for you to worry.  I really
like the @ syntax as it clearly alerts the reader that now something
special is going on.

OK, I now accept why the use of whitespace separators is liked (although I still dislike having whitespace being significant on general principals! ;-) ),
but the documentation doesn't seem to make that clear... it looks to me like this consequence of having the Julia macros work after the parsing, instead of
working on the text, like most other languages, seemed obvious to the implementors of Julia, and hence wasn't mentioned in the documentation.
Given that this is so different from the way most people would expect coming from other languages, I think it should be pointed out in big red letters in the documentation,
so as not to confuse newcomers to Julia.
I.e. explain why:
@doc """
My documentation
""" ->

works,
and
@doc """My documentation""" ->
works,
but
@doc """
My documentation
""""
->
does not... (and this example gets tricky to a newbie, because you don't get an error on @doc not having enough arguments, because @doc expression gives you the documentation, and @doc expression -> *sets* the documentation of the following object.)

Scott

Isaiah Norton

unread,
Apr 30, 2015, 11:59:00 AM4/30/15
to juli...@googlegroups.com
 it looks to me like this consequence of having the Julia macros work after the parsing, instead of
working on the text, like most other languages

Julia macros are syntactic like Lisp rather than textual like C. We say as much -- and emphasize the difference -- in the very first paragraph of the metaprogramming section of the manual:


(if there is ever any doubt of Julia's heritage, just type `julia --lisp`)

Matt Bauman

unread,
Apr 30, 2015, 12:01:54 PM4/30/15
to juli...@googlegroups.com
On Thursday, April 30, 2015 at 11:46:47 AM UTC-4, Scott Jones wrote:
Given that this is so different from the way most people would expect coming from other languages, I think it should be pointed out in big red letters in the documentation

Please do help us improve the manual!  As a new developer, you are in a unique position to see shortcomings in the manual that are no longer obvious to the more seasoned contributors.  Even future-you won't be as good at identifying this.

Scott Jones

unread,
Apr 30, 2015, 12:11:20 PM4/30/15
to juli...@googlegroups.com
On Apr 30, 2015, at 11:58 AM, Isaiah Norton <isaiah...@gmail.com> wrote:

 it looks to me like this consequence of having the Julia macros work after the parsing, instead of
working on the text, like most other languages

Julia macros are syntactic like Lisp rather than textual like C. We say as much -- and emphasize the difference -- in the very first paragraph of the metaprogramming section of the manual:

Yes, I saw that, and as I said, the *consequences* of that may seem obvious to the Julia implementors - it is not so obvious to someone coming fresh to Julia from C/C++/assembly…
As far as the Lisp heritage goes (I learned MacLisp before C, and was an undergraduate TA for the first Scheme course at MIT, so I know that as well), Lisp may be syntactic rather than textual, but
Lisp doesn’t have things like juxtoposition, infix operators, syntax like function(arg1, arg2, …), so it isn’t that confusing… the macro *will* end at the closing ), consistently.

Scott

Scott Jones

unread,
Apr 30, 2015, 12:12:31 PM4/30/15
to juli...@googlegroups.com
I’d like to, I’m not really a good writer, but I’ll make a good attempt at it! (and hopefully it will get accepted, if I haven’t p*ssed off too many of the core team! ;-) )

Scott

Tim Holy

unread,
Apr 30, 2015, 12:36:13 PM4/30/15
to juli...@googlegroups.com
You haven't. Keep the contributions coming!

With regards to the manual, simply noting what's confusing is half the battle.
As you may have noticed, most pull requests receive suggestions for changes,
and wording improvements on documentation are common.

--Tim

ele...@gmail.com

unread,
May 1, 2015, 2:37:48 AM5/1/15
to juli...@googlegroups.com


On Friday, May 1, 2015 at 2:12:31 AM UTC+10, Scott Jones wrote:
I’d like to, I’m not really a good writer, but I’ll make a good attempt at it! (and hopefully it will get accepted, if I haven’t p*ssed off too many of the core team! ;-) )

Scott


As an outsider I can say on behalf of the team, no matter how much you and they disagree, so long as everyone remains civil and acknowledges that there are other points of view, they welcome all contributions, which is one of the joys of Julia.

Cheers
lex
Reply all
Reply to author
Forward
0 new messages