# Dispatch on lambda functions

**URL:** <https://discourse.julialang.org/t/dispatch-on-lambda-functions/80064>\
**Category:** General Usage\
**Tags:** dispatch, multidispatch\
**Created:** [April 26, 2022, 2:09pm UTC](https://discourse.julialang.org/t/dispatch-on-lambda-functions/80064 "2022-04-26T14:09:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jj-404](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jj-404/32/34532_2.png) [@jj-404](https://discourse.julialang.org/u/jj-404)\
**Post date:** [April 26, 2022, 2:09pm UTC](https://discourse.julialang.org/t/dispatch-on-lambda-functions/80064/1 "2022-04-26T14:09:13Z")

</div>

Hi, is there a way to dispatch on lambda functions? It seems that lambda’s don’t have a specific type for themselves (unlike `Functions`), but are rather uniquely typed with a counter (`var"#29#30"`). I would like to dispatch like in the following MWE (or at least to be able to reference `var"..." types`):

```julia
λ = () -> 2 # typeof(λ) == var"#29#30"
x = 2

fun(x) = x
fun(x::Lambda) = x()

fun(x) # == 2
fun(λ) # == 2

```

Thanks in advance

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [April 26, 2022, 2:16pm UTC](https://discourse.julialang.org/t/dispatch-on-lambda-functions/80064/2 "2022-04-26T14:16:50Z")

</div>

```julia
julia> fun(x::typeof(λ)) = x()
fun (generic function with 2 methods)

julia> fun(λ)
2

```

---

<div class="post-metadata">

**Author:** ![jj-404](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jj-404/32/34532_2.png) [@jj-404](https://discourse.julialang.org/u/jj-404)\
**Post date:** [April 26, 2022, 2:18pm UTC](https://discourse.julialang.org/t/dispatch-on-lambda-functions/80064/3 "2022-04-26T14:18:19Z")

</div>

thanks for your answer, I should have made clearer that I want to dispatch on the fact that the input is any lambda function, not only this particular one

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [April 26, 2022, 2:19pm UTC](https://discourse.julialang.org/t/dispatch-on-lambda-functions/80064/4 "2022-04-26T14:19:34Z")

</div>

> [@jj-404](#):
>
> I want to dispatch on the fact that the input is any lambda function, not only this particular one

Lambdas are not special - they’re regular `<: Function`s (implemented as callable structs that subtype `Function`). They just don’t have a convenient name, but rather a generated one.

---

<div class="post-metadata">

**Author:** ![jj-404](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jj-404/32/34532_2.png) [@jj-404](https://discourse.julialang.org/u/jj-404)\
**Post date:** [April 26, 2022, 2:21pm UTC](https://discourse.julialang.org/t/dispatch-on-lambda-functions/80064/5 "2022-04-26T14:21:11Z")

</div>

great, thanks, I didn’t know lambdas were `<: Function` !

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [April 26, 2022, 2:22pm UTC](https://discourse.julialang.org/t/dispatch-on-lambda-functions/80064/6 "2022-04-26T14:22:05Z")

</div>

Note that this doesn’t allow you to distinguish lambdas from named functions:

```julia
julia> typeof(+) <: Function
true

```

`Function` is an abstract type after all and every function in julia has its own type, that’s `<: Function` itself.
