thautwarm / MLStyle.jl

Julia functional programming infrastructures and metaprogramming facilities

Home Page:https://thautwarm.github.io/MLStyle.jl/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.

  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[])