Saturday, 12 March 2016

Julia: Parametric methods


In Julia, you can specify a type parameter optionally. Type parameters are specified immediately after the method name and before the parameter tuple.
julia> function process_data{T}(a::T, b::T)
           println("Inside type parameter method")
       end
process_data (generic function with 1 method)

julia> function process_data(a, b)
           println("Inside generic method")
       end
process_data (generic function with 2 methods)

julia> 

julia> process_data(10, 20)
Inside type parameter method

julia> process_data(10, 20.04)
Inside generic method

julia> process_data("Hi", "Hello")
Inside type parameter method

julia> process_data("Hi", 123)
Inside generic method

function process_data{T}(a::T, b::T)
Above method applies whenever both arguments are of the same concrete type, regardless of what type that is, while the second method ‘function process_data(a, b)’ acts as generic method.



Previous                                                 Next                                                 Home

No comments:

Post a Comment