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

support kwargs in @page macro #222

Merged
merged 15 commits into from
Sep 14, 2023
Merged

support kwargs in @page macro #222

merged 15 commits into from
Sep 14, 2023

Conversation

hhaensel
Copy link
Member

@hhaensel hhaensel commented Sep 8, 2023

Currently different methods of the @page macro are distinguishable only by the number of arguments, requiring the argument order url, view, layout, model, context.
With the new macro tool expressions_to_args() we can support a kwarg-like macro call, e.g.

model = @init
@page("/", ui, model = model)

for global models without the requirement to specify a layout.

EDIT: Note that the ;-Syntax with auto duplication for kwargs is not supported in macros

@hhaensel
Copy link
Member Author

hhaensel commented Sep 8, 2023

Still thinking how we could support passing of more arguments to the init function ...

@hhaensel
Copy link
Member Author

hhaensel commented Sep 8, 2023

Now we can do:

@init(debounce = 10)

In order to change the settings after they have been already set, it is important to clear the cached route and DEPS. For this I introduced clear_cache() and @clear_cache

@clear_cache
@init(debounce = 12)

@hhaensel
Copy link
Member Author

hhaensel commented Sep 8, 2023

Before we think about merging, I'm going to support semicolon parameter syntax

@hhaensel
Copy link
Member Author

hhaensel commented Sep 9, 2023

Well, it's done. Needed to find out that macros only catch parameter syntax consistently when there is no positional argument in the definition.
Look at this:

macro f(x, expressions...)
    @show x expressions
    return nothing
end

@f(a, b="b"; c=c)
# x = :($(Expr(:parameters, :($(Expr(:kw, :c, :c))))))
# expressions = (:a, :(b = "b"))

@essenciary
Copy link
Member

Interesting, I've never attempted this. With functions the keyword args are collected in the named tuple.

julia> f(args...; kwargs...) = print(args, kwargs)
f (generic function with 1 method)

julia> f(2, a=:a; b=3)
(2,)Base.Pairs{Symbol, Any, Tuple{Symbol, Symbol}, NamedTuple{(:a, :b), Tuple{Symbol, Int64}}}(:a => :a, :b => 3)

Because it's passed before the ;, the a=:a argument is passed as one entity, a named tuple.

@hhaensel
Copy link
Member Author

hhaensel commented Sep 9, 2023

Yeah, the functions do aggregate args and kwargs, I've used that a thousand times.
I even investigated macro syntax when I wrote my bits of the onchange parser. But I didn't realise that distribution to args goes wrong.
I'll ask this on discourse...

EDIT:

  • corrected typo
  • seems to be known, see discourse

@PGimenez
Copy link
Member

One thought. Should we enforce strict ordering so that kw arguments? (if it can be done) Now you can do something like

@page("/model2", model = Model, ui )

which works since the macro rearranges the parameters inside. This may suggest that order is not important, but if one were to try

@page(ui,  model = Model, "/model2" )

it wouldn't work.

Other than this, I like the changes and it's very useful for when you want to use multiple models without specifying a layout.

@hhaensel
Copy link
Member Author

I think it is known from functions that the order of positional arguments is important while the oder of kwargs is not.
That's exactly the behaviour I copied.

@hhaensel hhaensel merged commit 556ac21 into master Sep 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants