-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
provide a line continuation syntax for nicer macro calls #18612
Comments
Currently you can just put the code in a @simd @inbounds begin
for i = 1:10
x[i] = i + 2 + a*sin(1.0)
y[i] = i + y[i]
end
end or @simd @inbounds(
for i = 1:10
x[i] = i + 2 + a*sin(1.0)
y[i] = i + y[i]
end) IMO both of these are quite readable, or at least not less so than |
Right I'd argue the first is ugly and verbose the second is passable @simd @inbounds @othermac #=
=#for i = 1:10
x[i] = i + 2 + a*sin(1.0)
y[i] = i + y[i]
end) Don't get hung up on E.g. using @inline @inbounds @another @one @for @fun \
function foo(x)
x = x + 1
x += 1
return x + 1
end
@simd @inbounds @othermac \
for i = 1:10
x[i] = i + 2 + a*sin(1.0)
y[i] = i + y[i]
end In any case, if this feels unnecessary by any of the higher ups feel free to close. The strongest argument I can find against this is that at most, code blocks shouldn't need more than 3 macros and so readability shouldn't be that impacted that a new syntax is needed. |
Purely out of curiosity: What would happen if there was no line continuation syntax at all, that the macros were simply allowed to discard the leading newline and process the next valid expression? (Asking totally from a position of ignorance here.) |
@okvs I like that idea a lot -- it would allow for using macros in Python-like decorator style to annotate the block it belongs to -- but I think it introduces an ambiguity right now when a macro is defined with no arguments. I wonder how often macros without arguments are used. If the rules were changed to require |
So the problem would be macros that take zero arguments. If macros could somehow tolerate being fed too many inputs, that could work, I guess, but I really don't know what I'm talking about here... I would just have liked to be able to do something like this: @simd @inbounds
for i = 1:10
x[i] = i + 2 + a*sin(1.0)
y[i] = i + y[i]
end |
@okvs That would mean that macros that take no arguments would need to be called as |
No you can't do that (at least not always): julia> @simd @inbounds(
for i = 1:10
x[i] = i + 2 + a*sin(1.0)
y[i] = i + y[i]
end)
Base.SimdLoop.SimdError("for loop expected")
julia> @simd @inbounds begin
for i = 1:10
x[i] = i + 2 + a*sin(1.0)
y[i] = i + y[i]
end
end
Base.SimdLoop.SimdError("for loop expected") Also what happens with multiple arguments? You are only taking into consideration one arg. |
The julia> @simd @inbounds for i = 1:10
println(i)
end
ERROR: Base.SimdLoop.SimdError("for loop expected") |
Indeed, although |
reopening, the resolution/decision of #29273 should determine how this issue is closed. |
I think it's fairly clear from #29273 that the specific implementation proposed there won't be accepted. But I still regularly want this feature and after some experience using the alternative of "commenting out the linebreak" I still find that ugly and distracting. To copy/summarize some thinking from #29273 (comment): I feel this whole thing might still fly if we could come up with a compelling enough pair of ascii chars for line continuation. Some desirable properties:
There's not many viable character pairs which fit these criteria. @inline @inbounds @another @one @for @fun .#
function foo(x)
x = x + 1
x += 1
return x + 1
end |
If a macrocall always consumed a newline if present (even when it turns out that macrocall takes zero arguments), would that introduce an ambiguity or break existing code? |
I'm confused by this comment. Macro calls currently use the newline to terminate the argument list. That's a pretty critical feature of the syntax! |
D'oh, sorry, I see what the problem is now. |
I often run into this issue (and the related issue #27533). I think the proposals suggested in #29273 (comment) are quite good. I took the liberty of implementing this in Julia's parser using the character combinations What are the opinions on this? |
Well I still think I had some good points about the design tradeoffs in #29273 (comment) but it didn't generate much discussion 🤷♂️ I'm certainly happy to continue the discussion here. Generally I think it would be helpful to open a new PR for Implementation details... I think |
Another almost non-breaking option would be Edit: actually there's some other cases where it's allowed. Eg, |
Was Edit: So, it'd be breaking because this works: julia> @#
inline f(x) = x
f (generic function with 1 method) But do people use it? It is parsed like this julia> """
@#
a
""" |> Meta.parse |> dump
Expr
head: Symbol macrocall
args: Array{Any}((2,))
1: Symbol @a
2: LineNumberNode
line: Int64 2
file: Symbol none |
So, it looks like you can implement line continuation as a macro macro c(x)
esc(x)
end
@#
c @inline @#
c @inbounds @#
c f(x, i) = x[i] |
I remember thinking about I don't think anyone would be worried if
|
OK, but thinking a bit more seriously, I think @inbounds @#
f(x)[i] = 1 I think a new syntax should just throw an error in old parser as much as possible. I think it's too easy to get a currently valid code with |
To get the full beauty of Fortran shouldn't you have that It's true that having both backward and forward compatibility would be nice. It wouldn't be great if the older parsers would happily interpret a new line continuation syntax as valid syntax meaning something completely different. It's true The implementation would be the same in any case. |
currently
Proposal: introduce a line continuation syntax, e.g.
#\
(like how C uses\
or we could borrow from C and straight up use\
, however#
is nice since it already signifies a comment so it's more familiar) so that the following worksproposal
Motivation: it becomes hard and ugly (personal opinion) to identify functions when prefixed with multiple macros. Arguably the fact I'm defining a function should be front and center. In the current syntax one has to follow the chain of macro calls before being able to reach and identify the function signature. This proposal places the function signature front and center and allows the macro calls to placed above the signature.
More examples
currently
proposal
Again here it's hard to quickly identify the for loop due to the macro calls that precede the for loop definition. In the proposed syntax it's no more difficult than if no macros were used and one can quickly identify the for loop.
The ultimate goal here is to keep the syntax simple and pretty, even if one has to use several macro calls
The text was updated successfully, but these errors were encountered: