Give a useful example of `let` patterns
jariji opened this issue · comments
@match 1 begin
let x = 1 end => x
end
is the only example, which doesn't show why I would want to use it.
-
Creating (local) bindings during matching is a function that cannot be expressed by other syntax.
-
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[])