# Why pattern matching on map key in function arguments causes an unbound error?

**URL:** <https://erlangforums.com/t/why-pattern-matching-on-map-key-in-function-arguments-causes-an-unbound-error/1979>\
**Category:** Questions / Help\
**Tags:** maps, erlang\
**Created:** [October 26, 2022, 9:33am UTC](https://erlangforums.com/t/why-pattern-matching-on-map-key-in-function-arguments-causes-an-unbound-error/1979 "2022-10-26T09:33:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![williamthome](https://erlangforums.com/user_avatar/erlangforums.com/williamthome/32/471_2.png) [@williamthome](https://erlangforums.com/u/williamthome)\
**Post date:** [October 26, 2022, 9:33am UTC](https://erlangforums.com/t/why-pattern-matching-on-map-key-in-function-arguments-causes-an-unbound-error/1979/1 "2022-10-26T09:33:38Z")

</div>

Can someone explain why the pattern match in function arguments on map keys is invalid? e.g.:

```nohighlight
1> F = fun(#{key := Key}, #{Key := Value}) -> Value end.
* 1:16: variable 'Key' is unbound

```

But this is valid:

```nohighlight
2> F = fun(#{key := Key}, Map) -> #{Key := Value} = Map, Value end.
#Fun<erl_eval.41.3316493>

```

---

<div class="post-metadata">

**Author:** ![kjnilsson](https://erlangforums.com/user_avatar/erlangforums.com/kjnilsson/32/450_2.png) [@kjnilsson](https://erlangforums.com/u/kjnilsson)\
**Post date:** [October 26, 2022, 10:50am UTC](https://erlangforums.com/t/why-pattern-matching-on-map-key-in-function-arguments-causes-an-unbound-error/1979/2 "2022-10-26T10:50:49Z")

</div>

IIRC there are no guarantees that bindings will be evaluated left to right as written in the function head. If there was it would put restrictions on how the function head would be translated into core erlang and possibly disallow certain optimisations (like evaluating the likely cheapest pattern match first irrespective of where in the function head it occurs).

Or something a bit like that.

---

<div class="post-metadata">

**Author:** ![williamthome](https://erlangforums.com/user_avatar/erlangforums.com/williamthome/32/471_2.png) [@williamthome](https://erlangforums.com/u/williamthome)\
**Post date:** [October 26, 2022, 3:42pm UTC](https://erlangforums.com/t/why-pattern-matching-on-map-key-in-function-arguments-causes-an-unbound-error/1979/3 "2022-10-26T15:42:17Z")

</div>

Thanks, @kjnilsson!
