How to annotate anonymous function parameter type

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:

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

And the function would be called like this:

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

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

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

createStringArrayFromLambda(lampdaEypression::Function)

Is that what you are looking for?

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

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 .

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

@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.

1 Like

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

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.

3 Likes

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…

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

4 Likes