# Wrapper functions guidlines

**URL:** <https://discourse.julialang.org/t/wrapper-functions-guidlines/27387>\
**Category:** General Usage\
**Tags:** question, type\
**Created:** [August 10, 2019, 11:01am UTC](https://discourse.julialang.org/t/wrapper-functions-guidlines/27387 "2019-08-10T11:01:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amin\_Yahyaabadi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amin_yahyaabadi/32/9826_2.png) [@Amin\_Yahyaabadi](https://discourse.julialang.org/u/Amin_Yahyaabadi)\
**Post date:** [August 10, 2019, 11:01am UTC](https://discourse.julialang.org/t/wrapper-functions-guidlines/27387/1 "2019-08-10T11:01:43Z")

</div>

As part of one of my [JuliaMatlab](https://github.com/juliamatlab) packages, [MatLang](https://github.com/juliamatlab/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?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 10, 2019, 11:30am UTC](https://discourse.julialang.org/t/wrapper-functions-guidlines/27387/2 "2019-08-10T11:30:31Z")

</div>

1. Same as for writing efficient code in general. Start with [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-tips-1)

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](https://docs.julialang.org/en/v1/manual/methods/#Design-Patterns-with-Parametric-Methods-1)

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

4. `const zerosM = zeros`

5. just throw an error, eg

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.

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [August 10, 2019, 10:47pm UTC](https://discourse.julialang.org/t/wrapper-functions-guidlines/27387/3 "2019-08-10T22:47:17Z")

</div>

> [@Amin\_Yahyaabadi](#):
>
> 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` .

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](https://github.com/tkf/Transducers.jl)) as an alternative, or you can simply use the package leading ( `MatLang.zeros`)

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [August 10, 2019, 10:49pm UTC](https://discourse.julialang.org/t/wrapper-functions-guidlines/27387/4 "2019-08-10T22:49:43Z")

</div>

> [@Amin\_Yahyaabadi](#):
>
> 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?

you can add error checking in your own code if you want, see: [Exception Handling](https://docs.julialang.org/en/v1/manual/control-flow/#Exception-Handling-1)

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [August 10, 2019, 10:54pm UTC](https://discourse.julialang.org/t/wrapper-functions-guidlines/27387/5 "2019-08-10T22:54:09Z")

</div>

> [@Amin\_Yahyaabadi](#):
>
> How can I pass all the input arguments to an inner function? (multiple arguments)

this example can help

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

```

---

<div class="post-metadata">

**Author:** ![Amin\_Yahyaabadi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amin_yahyaabadi/32/9826_2.png) [@Amin\_Yahyaabadi](https://discourse.julialang.org/u/Amin_Yahyaabadi)\
**Post date:** [August 11, 2019, 9:46am UTC](https://discourse.julialang.org/t/wrapper-functions-guidlines/27387/6 "2019-08-11T09:46:56Z")

</div>

> [@Wrapper functions guidlines](https://discourse.julialang.org/t/wrapper-functions-guidlines/27387/2):
>
> Same as for writing efficient code in general. Start with [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-tips-1) 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](https://docs.julialang.org/en/v1/manual/methods/#Design-Patterns-with-Parametric-Methods-1) outer(args...) = inner(args...) const zerosM = zeros just throw an error, eg zerosM(\_...) = error("Toto, I have a feeling we'…

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.

---

<div class="post-metadata">

**Author:** ![Amin\_Yahyaabadi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amin_yahyaabadi/32/9826_2.png) [@Amin\_Yahyaabadi](https://discourse.julialang.org/u/Amin_Yahyaabadi)\
**Post date:** [August 11, 2019, 9:47am UTC](https://discourse.julialang.org/t/wrapper-functions-guidlines/27387/7 "2019-08-11T09:47:35Z")

</div>

> [@Wrapper functions guidlines](https://discourse.julialang.org/t/wrapper-functions-guidlines/27387/5):
>
> this example can help function mysum(args...) length(args)==1 && throw(ArgumentError("I can't sum just one element")) return sum(args...) end

Thank you for your answers! It really helps.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 11, 2019, 11:11am UTC](https://discourse.julialang.org/t/wrapper-functions-guidlines/27387/8 "2019-08-11T11:11:45Z")

</div>

> [@Amin\_Yahyaabadi](#):
>
> I am trying to attract all the people in universities and academia to use Julia instead of Matlab.

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.
