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 typevars in do expressions #437

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Conversation

sgaure
Copy link

@sgaure sgaure commented Jun 7, 2024

There are several ways to create functions in julia. One of them is the do syntax. You can't dispatch on do functions, but now and then the need comes up to capture the types of variables. Although this is easy with typeof inside the function body, it turns out to be quite easy to enable the use of where clauses in the do syntax. This would make a do function similar to the other function definitions.

The reason it's easy is that such expressions are already being parsed, but a minor detail prevents it from being accepted as a valid expression:

julia-1.10.4> Meta.show_sexpr(:(f() do (x::T) where {T}; T(x) end))
(:do, (:call, :f), (:->, (:tuple, (:where, (:(::), :x, :T), :T)), (:block,
      :(#= REPL[1]:1 =#),
      (:call, :T, :x)
    )))

The where clause becomes encapsulated in a tuple. The reason is that after the parser sees do, it looks for a comma separated list. After the list is parsed, i.e. on ; or linefeed, the list is encapsulated in a tuple. This means that the where clause becomes an argument to the function, which fails as a syntax error. By modifying the parsing of do expressions to avoid inserting tuple if the newly parsed list is a where, the parse result becomes:

julia-this-PR> Meta.show_sexpr(:(f() do (x::T) where {T}; T(x) end))
(:do, (:call, :f), (:->, (:where, (:(::), :x, :T), :T), (:block,
      :(#= REPL[1]:1 =#),
      (:call, :T, :x)
    )))

This is a valid :-> expression, just like an anonymous function definition ((x::T) where T) -> T(x) currently is.

The PR is a one-line change to parser.jl, isolated to parsing what follows do. It does not enable any new or very useful functionality, it merely will make functions defined in do quite similar to other function definitions.

I don't think this change will break anything, as the construction currently results in a ERROR: syntax: ... failure.

Allows constructs like
f() do (x::T) where T; body end
It's already being parsed, but the where clause is put into a tuple, causing a syntax error downstream. This change just avoids putting it in a tuple.
@LilithHafner
Copy link
Member

I think it is best for proposed syntax changes to start as or have an associated issue in the JuilaLang/julia repo for viability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants