# Controlling the type of anonymous functions

**URL:** <https://discourse.julialang.org/t/controlling-the-type-of-anonymous-functions/26264>\
**Category:** General Usage\
**Created:** [July 11, 2019, 7:36pm UTC](https://discourse.julialang.org/t/controlling-the-type-of-anonymous-functions/26264 "2019-07-11T19:36:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathaniel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathaniel/32/37854_2.png) [@Nathaniel](https://discourse.julialang.org/u/Nathaniel)\
**Post date:** [July 11, 2019, 7:36pm UTC](https://discourse.julialang.org/t/controlling-the-type-of-anonymous-functions/26264/1 "2019-07-11T19:36:33Z")

</div>

I have a function with the following form:

```julia
function make_func()
	return x -> x*10
end

```

I would like to change it so that it will return a function that takes Cint and returns Cint, in order to pass it to C code as a callback using ccall. I expected this to work, but it doesn’t. (It gives a syntax error, unless I remove the type assertion on the parameter x.)

```julia
function make_func()
	f = function(x::Cint)::Cint
		return x*10
	end
	return f
end

```

Is there a way that I can specify both the input and return types of an anonymous function?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [July 11, 2019, 7:47pm UTC](https://discourse.julialang.org/t/controlling-the-type-of-anonymous-functions/26264/2 "2019-07-11T19:47:21Z")

</div>

Edit: I misread the question. The post below is probably not very helpful, sorry.

Original post:

You don’t actually need to restrict the anonymous function at all. Instead, you should use `@cfunction` to generate the C-compatible function pointer:

```julia
julia> function make_func()
         x -> x * 10
       end
make_func (generic function with 1 method)

julia> @cfunction(make_func, Cint, (Cint,))
Ptr{Nothing} @0x00007f14ade9a230

```

See [Calling C and Fortran Code · The Julia Language](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/index.html#Creating-C-Compatible-Julia-Function-Pointers-1)

---

<div class="post-metadata">

**Author:** ![Nathaniel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathaniel/32/37854_2.png) [@Nathaniel](https://discourse.julialang.org/u/Nathaniel)\
**Post date:** [July 11, 2019, 7:56pm UTC](https://discourse.julialang.org/t/controlling-the-type-of-anonymous-functions/26264/3 "2019-07-11T19:56:21Z")

</div>

I’m having a hard time making sense of that. I don’t want to turn `make_func` into a cfunction, I want to turn the function returned by `make_func` into a cfunction, i.e.

```julia
func = make_func()
func_c = @cfunction($func, Cint, (Cint,))

```

If I try to pass `make_func` to `@cfunction` directly, it gives the error I would expect:

```julia
ERROR: LoadError: MethodError: no method matching make_func(::Int32)

```

---

<div class="post-metadata">

**Author:** ![Nathaniel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathaniel/32/37854_2.png) [@Nathaniel](https://discourse.julialang.org/u/Nathaniel)\
**Post date:** [July 11, 2019, 8:01pm UTC](https://discourse.julialang.org/t/controlling-the-type-of-anonymous-functions/26264/4 "2019-07-11T20:01:11Z")

</div>

I note also that I can get around this by doing the following:

```julia

	func = make_func()

	# this function exists only to control the type of func()
	function func_wrapper(x::Cint)::Cint
		return func(x)
	end

	func_wrapper_c = @cfunction($func_wrapper, Cint, (Cint,))

```

This appears to work correctly, and to behave as a C function of type `int (*f)(int)`, as expected. But it’s weird and hacky to have to create the `func_wrapper` function just to control the type of the anonymous function. I’m asking how to do that in a more paradigmatic way.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [July 11, 2019, 8:31pm UTC](https://discourse.julialang.org/t/controlling-the-type-of-anonymous-functions/26264/5 "2019-07-11T20:31:55Z")

</div>

Looks like the anonymous function form is bugged in how it parses the return type assertions — it puts them on the argument instead! Tracked as issue [#32557](https://github.com/JuliaLang/julia/issues/32557):

```julia
julia> :(function (x)::Cint end)
:(function (x::Cint,)
      #= REPL[39]:1 =#
  end)

```

As a workaround, you can just use a named function instead — it’s still scoped inside your function and forms a closure if you capture any variables. Or you can just manually convert things to `Cint` yourself when you return — the function return declaration is just a handy way to annotate that.

---

<div class="post-metadata">

**Author:** ![Nathaniel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathaniel/32/37854_2.png) [@Nathaniel](https://discourse.julialang.org/u/Nathaniel)\
**Post date:** [July 11, 2019, 8:44pm UTC](https://discourse.julialang.org/t/controlling-the-type-of-anonymous-functions/26264/6 "2019-07-11T20:44:57Z")

</div>

Great, many thanks. Thanks for the tip about named functions! So here is working code that does what I wanted:

```julia
function make_func()
	function f(x::Cint)::Cint
		x*10
	end
	return f
end

function main()

	func = make_func()

	func_wrapper_c = @cfunction($func, Cint, (Cint,))
	result = ccall((:myTestFunction, "myTestLib"), Int32, (Ptr{Cvoid},), func_wrapper_c)

	println(result)

end

main()

```
