# How to annotate anonymous function parameter type

**URL:** <https://discourse.julialang.org/t/how-to-annotate-anonymous-function-parameter-type/62124>\
**Category:** General Usage\
**Tags:** question\
**Created:** [May 31, 2021, 10:29am UTC](https://discourse.julialang.org/t/how-to-annotate-anonymous-function-parameter-type/62124 "2021-05-31T10:29:09Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![peteole](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peteole/32/209790_2.png) [@peteole](https://discourse.julialang.org/u/peteole)\
**Post date:** [May 31, 2021, 10:29am UTC](https://discourse.julialang.org/t/how-to-annotate-anonymous-function-parameter-type/62124/1 "2021-05-31T10:29:09Z")

</div>

Hi,  
I am wondering how you can annote the signature of a function that is passed as a parameter.  
For example if I wanted to define a (quite useless) method which returns an array of strings which are computed from an integer, I would do the following:

```julia
function createStringArrayFromLambda(lampdaEypression)
   return lambdaExpression.(0:10)
end

```

And the function would be called like this:

```julia
myStringArray=createStringArrayFromLambda((n::Int)->"the number is"*string(n))

```

How would you annotate the type of the “lambdaExpression” parameter?

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [May 31, 2021, 10:48am UTC](https://discourse.julialang.org/t/how-to-annotate-anonymous-function-parameter-type/62124/2 "2021-05-31T10:48:08Z")

</div>

The following would constrain your input to be a function of any type:

`createStringArrayFromLambda(lampdaEypression::Function)`

Is that what you are looking for?

---

<div class="post-metadata">

**Author:** ![peteole](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peteole/32/209790_2.png) [@peteole](https://discourse.julialang.org/u/peteole)\
**Post date:** [May 31, 2021, 1:20pm UTC](https://discourse.julialang.org/t/how-to-annotate-anonymous-function-parameter-type/62124/3 "2021-05-31T13:20:36Z")

</div>

Yeah, almost. I also want to annotate the signature of the lambda expression

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [May 31, 2021, 1:32pm UTC](https://discourse.julialang.org/t/how-to-annotate-anonymous-function-parameter-type/62124/4 "2021-05-31T13:32:13Z")

</div>

This is not possible. Unless you create your own type that codifies such information and wraps callable objects and that would be a major hassle .

---

<div class="post-metadata">

**Author:** ![peteole](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peteole/32/209790_2.png) [@peteole](https://discourse.julialang.org/u/peteole)\
**Post date:** [May 31, 2021, 1:43pm UTC](https://discourse.julialang.org/t/how-to-annotate-anonymous-function-parameter-type/62124/5 "2021-05-31T13:43:38Z")

</div>

This is a bit sad, isn’t it? Or is there a reason why this should be impossible?

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [May 31, 2021, 1:49pm UTC](https://discourse.julialang.org/t/how-to-annotate-anonymous-function-parameter-type/62124/6 "2021-05-31T13:49:26Z")

</div>

@peteole you want your function `createStringArrayFromLambda(lampdaEypression)` to only accept a specific lambda signature?

There is maybe a confusion regarding functions vs methods: what you define here is a function that accepts a _function_, not a function that accepts a specific method. It’s only when you call the function (`lampdaEypression`) that Julia determines which method to call from that function.

You could write a function that accepts a `Method`, but there is no nice way to invoke the method (that’s not something people normally do). And a `Method` is not an isbits type so you cannot dispatch on it.

You could pass as second argument a tuple of types that represents the signature of the method to call (which you can use with `invoke`). You could dispatch on that. But that’s probably not the kind of solution you want.

---

<div class="post-metadata">

**Author:** ![peteole](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peteole/32/209790_2.png) [@peteole](https://discourse.julialang.org/u/peteole)\
**Post date:** [May 31, 2021, 2:01pm UTC](https://discourse.julialang.org/t/how-to-annotate-anonymous-function-parameter-type/62124/7 "2021-05-31T14:01:29Z")

</div>

> [@sijo](#):
>
> @peteole you want your function `createStringArrayFromLambda(lampdaEypression)` to only accept a specific lambda signature?

Yes, that’s exactly what I want to do.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [May 31, 2021, 2:26pm UTC](https://discourse.julialang.org/t/how-to-annotate-anonymous-function-parameter-type/62124/8 "2021-05-31T14:26:35Z")

</div>

This seems to go against the design of the language. Even a lambda can have multiple methods (multiple signatures).

Is there a concrete problem you’re tyring to solve? Maybe we can find another approach.

---

<div class="post-metadata">

**Author:** ![peteole](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peteole/32/209790_2.png) [@peteole](https://discourse.julialang.org/u/peteole)\
**Post date:** [May 31, 2021, 5:14pm UTC](https://discourse.julialang.org/t/how-to-annotate-anonymous-function-parameter-type/62124/9 "2021-05-31T17:14:32Z")

</div>

> [@sijo](#):
>
> Is there a concrete problem you’re tyring to solve?

Well, not really. I recently noticed the existence of julia so I read a lot about the language design in the recent days and I really like it. However I do believe the lack of a matured IDE is currently breaking down the spread of the language. I really believe you need some sort of type information in a good IDE to make it convenient to use. This is why I started to think about ways to infer types in some scenarios. One of them was the “map” function with a lambda expression which returns a special type.

However maybe there is a way to make use of julia’s interactive nature to achieve such results? I was wondering if one could make up a routine which feeds sample values into methods and like this step by step observes variable types? This could simplify things a lot and maybe one could even infer information like matrix dimensions or example “input-output-pairs” from this…

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [May 31, 2021, 6:53pm UTC](https://discourse.julialang.org/t/how-to-annotate-anonymous-function-parameter-type/62124/10 "2021-05-31T18:53:49Z")

</div>

I think you’re looking for [GitHub - aviatesk/JET.jl: An experimental code analyzer for Julia, no need for additional type annotations](https://github.com/aviatesk/JET.jl). It’s in-progress but impressive work and @aviatesk apparently wants to integrate it in IDEs 🙂
