Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

Method dispatch

$
0
0

Julia is a lot about multiple dispatching and I am having trouble to understand what an idiomatic way of using mulitple dispatch is.

My question is for small methods if I should define two different methods or if I should just use if else statements? (i.e. if typeof(...)...)

Example: I want to format/convert numbers or arrays of numbers into a string. Is the below approach reasonable? Or what is an idiomatic way to deal with the two cases (i) a single number and (ii) an array of things (e.g. numbers).

function _format_params(para::Number)
    string(para)
end

function _format_params(para::Array)
    join(para,", ")
end

Thanks for explaining.

5 posts - 5 participants

Read full topic


Viewing all articles
Browse latest Browse all 2795

Latest Images

Trending Articles



Latest Images