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

Give a useful example of let patterns #175

Open
jariji opened this issue Feb 9, 2024 · 1 comment
Open

Give a useful example of let patterns #175

jariji opened this issue Feb 9, 2024 · 1 comment

Comments

@jariji
Copy link
Contributor

jariji commented Feb 9, 2024

@match 1 begin
    let x = 1 end => x
end

is the only example, which doesn't show why I would want to use it.

@thautwarm
Copy link
Owner

  1. Creating (local) bindings during matching is a function that cannot be expressed by other syntax.

  2. In terms of the real-world use case, sometimes you might want to use one body for multiple cases, then some cases need to create default values:

    f(expr) = @match expr begin
          :($f($(args...); $(kwargs...))) ||
          :($f($(args...))) && let kwargs = [] end =>
              (f, args, kwargs)
    end
    
     julia> f(:(func(a, b; c= 1)))
     (:func, Any[:a, :b], Any[:($(Expr(:kw, :c, 1)))])
    
     julia> f(:(1+2))
     (:+, Any[1, 2], Any[])

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

No branches or pull requests

2 participants