# The pattern matching wizard's thread

**URL:** <https://forum.dfinity.org/t/the-pattern-matching-wizards-thread/20134>\
**Category:** Language Support\
**Tags:** Motoko\
**Created:** [May 22, 2023, 2:21am UTC](https://forum.dfinity.org/t/the-pattern-matching-wizards-thread/20134 "2023-05-22T02:21:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [May 22, 2023, 2:21am UTC](https://forum.dfinity.org/t/the-pattern-matching-wizards-thread/20134/1 "2023-05-22T02:21:01Z")

</div>

I ran across this on one of @quint 's slides in motoko BootCamp:

 ![image](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/2X/a/a0dfe896df796b887881c2ff97ce36abab61f1d8.jpeg)

I tried it out and, sure enough, another tool for the tool box. I had no idea. [Internet Computer Loading](https://m7sm4-2iaaa-aaaab-qabra-cai.ic0.app/?tag=4058751971)

Anyone else have any patterns that they use that make their lives easier? I’m embarrassed to say that I still feel like I don’t understand the full power of these even two years into using Motoko.

When I look at the list at [Internet Computer Loading](https://internetcomputer.org/docs/current/motoko/main/language-manual#patterns-1), I feel that there are things like maybe I have more to learn.

I guess the above is the object pattern and was sitting there all along, but I needed an example to really understand it. I’m still not really sure what Annotated Pattern is.

Who has some cool examples of patterns?

I found the “or” pattern handy when I wanted to merge the behavior of the #Ledger and # ICRC1 types in the Origyn NFT:

```auto

transaction_id := switch(details.token){
        case(#ic(token)){
          switch(token.standard){
            case(#Ledger or #ICRC1){
              //D.print("found ledger");
              let checker = Ledger_Interface.Ledger_Interface();
              .....

```

It would be great to have a thread we can point people to that are just learning pattern matching so they have some good examples to look at.

---

<div class="post-metadata">

**Author:** ![Gekctek](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/gekctek/32/6863_2.png) [@Gekctek](https://forum.dfinity.org/u/Gekctek)\
**Post date:** [May 22, 2023, 3:46am UTC](https://forum.dfinity.org/t/the-pattern-matching-wizards-thread/20134/2 "2023-05-22T03:46:16Z")

</div>

I was also unaware. Very cool pattern matching

---

<div class="post-metadata">

**Author:** ![icme](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/icme/32/4327_2.png) [@icme](https://forum.dfinity.org/u/icme)\
**Post date:** [May 22, 2023, 4:10am UTC](https://forum.dfinity.org/t/the-pattern-matching-wizards-thread/20134/3 "2023-05-22T04:10:37Z")

</div>

> [@skilesare](#):
>
> `case(#Ledger or #ICRC1){`

This is a nice trick. The only gotchya is that since there are still two potential variant matches, Motoko won’t allow you to deconstruct any of the variants that were matched inside that specific `or` case statement (i.e. if there’s a value inside one or both of those variants, you need another switch to narrow it down and extract the value).

---

<div class="post-metadata">

**Author:** ![quint](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/quint/32/40449_2.png) [@quint](https://forum.dfinity.org/u/quint)\
**Post date:** [May 22, 2023, 8:25am UTC](https://forum.dfinity.org/t/the-pattern-matching-wizards-thread/20134/4 "2023-05-22T08:25:55Z")

</div>

You can find the slides here:  
[https://motoko.academy/custom/1](https://motoko.academy/custom/1)

---

<div class="post-metadata">

**Author:** ![rossberg](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rossberg/32/795_2.png) [@rossberg](https://forum.dfinity.org/u/rossberg)\
**Post date:** [May 22, 2023, 8:36am UTC](https://forum.dfinity.org/t/the-pattern-matching-wizards-thread/20134/5 "2023-05-22T08:36:57Z")

</div>

> [@icme](#):
>
> > [@skilesare](#):
> >
> > `case(#Ledger or #ICRC1){`
> 
> This is a nice trick. The only gotchya is that since there are still two potential variant matches, Motoko won’t allow you to deconstruct any of the variants that were matched inside that specific `or` case statement (i.e. if there’s a value inside one or both of those variants, you need another switch to narrow it down and extract the value).

It’s a bit more nuanced. It totally allows you to _deconstruct_ as deep as you want to go inside an or-pattern. It merely doesn’t (currently) allow you to _extract_ a value. For example,

```auto
case (#aNumber(42) or #something(_, {field1 = ?"text"} or {field2 = 100}))

```

is totally supported, but

```auto
case (#aNumber(n) or #aPair(n, _))

```

is not, as you cannot bind a variable inside an or-pattern. Supporting this has always been on the list of features to come eventually, though, and is allowed in many other languages with pattern matching.
