# 'Pattern(...) \<-' always adds '.withFilter': Scala ritual

**URL:** https://forum.canton.network/t/pattern-always-adds-withfilter-scala-ritual/1463
**Category:** Outreach
**Tags:** scala
**Created:** [October 14, 2020, 3:46pm UTC](https://forum.canton.network/t/pattern-always-adds-withfilter-scala-ritual/1463 "2020-10-14T15:46:57Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Stephen](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/stephen/32/102_2.png) [@Stephen](https://forum.canton.network/u/Stephen)
#### Post date: [October 14, 2020, 3:46pm UTC](https://forum.canton.network/t/pattern-always-adds-withfilter-scala-ritual/1463/1 "2020-10-14T15:46:57Z")

</div>

DAML and Haskell users are used to writing irrefutable patterns on the left of `<-` in a `do` block, such as to destructure a tuple. However, if you write the same in Scala,

```haskell
for {
  (a, b) <- Some((1, 2))
} yield a + b

```

it will desugar like so:

```haskell
Some((1, 2))
  .withFilter(check$ifrefutable$1 => check$ifrefutable$1: @unchecked match {
    case ((a @ _), (b @ _)) => true
    case _ => false
  })
  .map(x$1 => x$1: @unchecked match {
    case ((a @ _), (b @ _)) => a + b
  })

```

## No annotations either

As with `case`, there are no type _annotations_ to the left of `<-`, only type _patterns_:

```haskell
for {x: Int <- List(1, 2)} yield x

// desugars to

List(1, 2)
  .withFilter(check$ifrefutable$1 => check$ifrefutable$1: @unchecked match {
    case (x @ (_: Int)) => true
    case _ => false
  })
  .map(x: Int => x)

```

## Fixing the pattern

The fix for destructuring is to separate the destructuring from the `<-` binding:

```haskell
for {
  ab <- Some((1, 2))
  (a, b) = ab
} yield a + b

```

which desugars like so:

```haskell
Some((1, 2))
  .map { ab => 
    val x$2 = ab: @unchecked match {
      case (x$1 @ ((a @ _), (b @ _))) => (x$1, a, b)
    }
    val x$1 = x$2._1
    val a = x$2._2
    val b = x$2._3
    (ab, x$1)
  }
  .map(x$3 => x$3: @unchecked match {
    case ((ab @ _), ((a @ _), (b @ _))) => a + b
  })

```

It’s longer, but it works with types that can’t be filtered, and for those that can, at least it won’t silently drop if your pattern turned out to be not-so-irrefutable.

## Fixing the annotation

For type annotations, the fix is to use an ascription on the expression instead:

```haskell
for {x <- List(1, 2): List[Int]} yield x

```

## Seeing how something desugars

If you are curious how any expression desugars in Scala, you can use `reify` in the REPL:

```haskell
scala> import reflect.runtime.universe.reify
import reflect.runtime.universe.reify

scala> reify { for {x <- List(1, 2): List[Int]} yield x }
res11: reflect.runtime.universe.Expr[List[Int]] = Expr[List[Int]]((List.apply(1, 2): `package`.List[Int]).map(((x) => x))(List.canBuildFrom))

scala> reify { 2 :: Nil }
res12: reflect.runtime.universe.Expr[List[Int]] =
Expr[List[Int]]({
  <synthetic> <artifact> val x$1 = 2;
  Nil.$colon$colon(x$1)
})

```

Note that the output is a printed syntax tree rather than valid Scala code; nevertheless it is a good way to figure out what some piece of higher-level syntax will do in Scala.

_Tested with Scala 2.12.11._
