Wrapper functions guidlines

As part of one of my JuliaMatlab packages, MatLang, I want to write wrapper functions that simulate Matlab like APIs.

  • What are some guidelines for writing efficient wrappers?

  • Is using chained if-else efficient defining different options/methods for a function? How can I fully benefit from multiple-dispatch and compile-time optimizations?

  • How can I pass all the input arguments to an inner function? (multiple arguments)

  • Is there any way to define a new name for a Julia function?
    This happens when the same functions exist in Julia and Matlab like zeros. In my package, I use suffix M, so zerosM is the same as zeros, I just want to write zerosM such way that is exactly the same as zeros.

  • Alternatively, is there any way to display an error to use the native function name without M for the functions that already exist in Julia?

  1. Same as for writing efficient code in general. Start with Performance Tips · The Julia Language

  2. No, an if ... elseif ... else ... end branch inside the function has nothing to do with methods, it is just a branch. May be fine, but can also be less flexible if you need to extend it. Various other design patterns are possible, see Methods · The Julia Language

  3. outer(args...) = inner(args...)

  4. const zerosM = zeros

  5. just throw an error, eg

    zerosM(_...) = error("Toto, I have a feeling we're not in Matlab anymore.")
    

Hope this helps. However, from your questions it looks like you are just learning Julia. Consider just using it as is, instead of tailoring it to resemble Matlab.

4 Likes

I have this “Problem”, with entropy (thermodynamics vs stadistics) but in your case is worse, as the functions do something similar. a suffix is a good solution. i saw capitalization (Transducer.jl) as an alternative, or you can simply use the package leading ( MatLang.zeros)

1 Like

you can add error checking in your own code if you want, see: Exception Handling

1 Like

this example can help

function mysum(args...)
length(args)==1 && throw(ArgumentError("I can't sum just one element"))
return sum(args...)
end
1 Like

Thank you for your answer. It was very helpful!

Regarding my purpose, I am trying to attract all the people in universities and academia to use Julia instead of Matlab. It is not just me.

Thank you for your answers! It really helps.

I think that some library functions having different names is the easiest part of the transition from Matlab (or any other similar language) to Julia, and is someting that should be tackled from the beginning.

Otherwise, it will be more difficult to ask for help, read code written by others, and contribute to packages.

1 Like