# MLStyle variadic match ast

**URL:** <https://discourse.julialang.org/t/mlstyle-variadic-match-ast/75021>\
**Category:** General Usage\
**Created:** [January 22, 2022, 5:21am UTC](https://discourse.julialang.org/t/mlstyle-variadic-match-ast/75021 "2022-01-22T05:21:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [January 22, 2022, 5:21am UTC](https://discourse.julialang.org/t/mlstyle-variadic-match-ast/75021/1 "2022-01-22T05:21:44Z")

</div>

This code works

```nohighlight
using MLStyle, MLStyle.Modules.AST
@match :( (a::Foo, b::Bar) ) begin
    :($n1::$t1, $n2::$t2) => (n1,n2)
end

```

but I want to do this with arbitrarily many variables, not just 2. I tried using `...` but it failed:

```julia
@match :( (a::Foo,b::Bar) ) begin
    :(($n1::$t1,)...) => (n1...,)
end # Error, non exhaustive

```

I also tried a simpler case with `@matchast` but it failed:

```julia
@matchast :( (a::Foo,) ) begin
    ($n::$t,) => n
end # Error, malformed template

```

@thautwarm might you have any suggestions?

---

<div class="post-metadata">

**Author:** ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)\
**Post date:** [January 24, 2022, 2:42pm UTC](https://discourse.julialang.org/t/mlstyle-variadic-match-ast/75021/2 "2022-01-24T14:42:43Z")

</div>

Sorry for the late reply.

MLStyle supports your use case:

```julia
julia> data = :( a::Foo, b::Bar, c :: Foo, d:: Bar )
:((a::Foo, b::Bar, c::Foo, d::Bar))

@match data begin
   :($([:($a :: $b) for (a, b) in pack]...), ) => pack
end

4-element Vector{Tuple{Any, Any}}:
 (:a, :Foo)
 (:b, :Bar)
 (:c, :Foo)
 (:d, :Bar)

```

I’d suggest using the `Expr` pattern because `:($(pat...), )` is not-that readable.

```julia
julia> @match data begin
          Expr(:tuple, [:($a :: $b) for (a, b) in pack]...) => pack
       end
4-element Vector{Tuple{Any, Any}}:
 (:a, :Foo)
 (:b, :Bar)
 (:c, :Foo)
 (:d, :Bar)

```
