# Do ? inside lambda functions

**URL:** <https://forum.dfinity.org/t/do-inside-lambda-functions/8665>\
**Category:** Developers\
**Created:** [November 18, 2021, 4:38am UTC](https://forum.dfinity.org/t/do-inside-lambda-functions/8665 "2021-11-18T04:38:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzxchiang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jzxchiang/32/2592_2.png) [@jzxchiang](https://forum.dfinity.org/u/jzxchiang)\
**Post date:** [November 18, 2021, 4:38am UTC](https://forum.dfinity.org/t/do-inside-lambda-functions/8665/1 "2021-11-18T04:38:14Z")

</div>

When we put a lambda function inside a `do ?` block, e.g. `Array.filter<T>(arr, func(ele) { ... })`, the compiler doesn’t let use a `!` inside the lambda because there’s no enclosing `do ?`.

That makes it hard to use some library functions inside a `do ?` block.

Is there an easy way around this I’m missing?

---

<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:** [November 18, 2021, 6:01am UTC](https://forum.dfinity.org/t/do-inside-lambda-functions/8665/2 "2021-11-18T06:01:36Z")

</div>

That’s right, you cannot access a `do?` block from inside an inner function (in the same way you cannot jump to an outer label or return from an outer function).

If this was allowed, the `!` could escape the `do?` block, because you could write examples like the following:

```auto
var f : ?Nat -> Nat = func(x : ?Nat) { 0 }; // dummy initialiser
let o = do? {
  f := func(x : ?Nat) { x! };
};
f(null); // what now?

```

and various other variations.

So we cannot allow it. There is no easy workaround, I’m afraid – `do?` blocks are convenient, but do not always apply.

---

<div class="post-metadata">

**Author:** ![jzxchiang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jzxchiang/32/2592_2.png) [@jzxchiang](https://forum.dfinity.org/u/jzxchiang)\
**Post date:** [November 18, 2021, 7:21am UTC](https://forum.dfinity.org/t/do-inside-lambda-functions/8665/3 "2021-11-18T07:21:49Z")

</div>

Ah I see, so the issue lies in the difference in where a function is defined and where it is called. Thanks for clarifying.

---

<div class="post-metadata">

**Author:** ![jzxchiang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jzxchiang/32/2592_2.png) [@jzxchiang](https://forum.dfinity.org/u/jzxchiang)\
**Post date:** [December 13, 2021, 12:37am UTC](https://forum.dfinity.org/t/do-inside-lambda-functions/8665/4 "2021-12-13T00:37:34Z")

</div>

Just to follow up on this, it seems like the Motoko compiler is totally fine with trapping (i.e. assert false) inside a lambda (i.e. inner function), and that will trap the executing canister method as expected. Can you confirm if that’s OK to do?

---

<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:** [December 13, 2021, 7:47am UTC](https://forum.dfinity.org/t/do-inside-lambda-functions/8665/5 "2021-12-13T07:47:36Z")

</div>

Yes, a trap can potentially occur at any point during execution (in Motoko or otherwise), and it aborts the current canister method.
