Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare a Julia function that returns a function with a specific signature

How can I declare a Julia function that returns a function with a specific signature. For example, say I want to return a function that takes an Int and returns an Int:

function buildfunc()::?????
   mult(x::Int) = x * 2
   return mult
end

What should the question marks be replaced with?

like image 813
Tarik Avatar asked Oct 18 '25 23:10

Tarik


1 Answers

You can use Function type for this purpose. From Julia documentation:

Function is the abstract type of all functions

function b(c::Int64)::Int64
        return c+2;
    end

function a()::Function
        return b;        
    end

Which prints:

julia> println(a()(2));

4

Julia will throw exception for Float64 input.

   julia> println(a()(2.0)); 

ERROR: MethodError: no method matching b(::Float64) Closest candidates are: b(::Int64)

like image 93
Patates Avatar answered Oct 22 '25 04:10

Patates