+1 for Ethan’s approach Any time that you would have used conditionals in a function to change behavior based on the number of arguments or their types, the Julian approach is to use multiple dispatch instead.
— John On Apr 1, 2014, at 7:29 AM, Ethan Anderes <[email protected]> wrote: > Hi Adrian: > > Not sure how long you've been working with Julia but I also was looking for > nargin from Matlab when I recently converted to Julia. I figure I would post > this for other newbies in my situation. I had a lot of Matlab code like the > following > > function y = foo(x, z) > if nargin > 1 > ... do something with x and z .. > else > .... do something with x ... > end > end > > Then I realized the multiple dispatch approach of Julia make this much more > clean. So in julia I would write > > function foo(x) > .... do something with x ... > end > function foo(x, z) > .... do something with x and z .. > end > > > Cheers, > Ethan > >
