# Can I do this syntax in Julia when specifying types and default values for functions?

**URL:** <https://discourse.julialang.org/t/can-i-do-this-syntax-in-julia-when-specifying-types-and-default-values-for-functions/73606>\
**Category:** General Usage\
**Tags:** matlab, functions\
**Created:** [December 25, 2021, 9:53am UTC](https://discourse.julialang.org/t/can-i-do-this-syntax-in-julia-when-specifying-types-and-default-values-for-functions/73606 "2021-12-25T09:53:03Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [December 25, 2021, 9:53am UTC](https://discourse.julialang.org/t/can-i-do-this-syntax-in-julia-when-specifying-types-and-default-values-for-functions/73606/1 "2021-12-25T09:53:03Z")

</div>

Hi!

The newest version of Matlab ([Function Argument Validation - MATLAB & Simulink - MathWorks Nordic](https://se.mathworks.com/help/matlab/matlab_prog/function-argument-validation-1.html)) has this capability:

```julia
function c = f(a, b,c)
    arguments
        a uint32
        b uint32
        c uint32 = a .* b
    end

    % Function code
    ...
end

```

I personally (note: my opinion) really like this structured syntax. Could I do something similar as this when making a julia function, instead of having to write it directly in the function definition?

Kind regards

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [December 25, 2021, 10:30am UTC](https://discourse.julialang.org/t/can-i-do-this-syntax-in-julia-when-specifying-types-and-default-values-for-functions/73606/2 "2021-12-25T10:30:57Z")

</div>

Yes. See `@assert` macro. Or you can just use your own if statements.

---

<div class="post-metadata">

**Author:** ![Paulo\_Jabardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paulo_jabardo/32/3196_2.png) [@Paulo\_Jabardo](https://discourse.julialang.org/u/Paulo_Jabardo)\
**Post date:** [December 25, 2021, 10:39am UTC](https://discourse.julialang.org/t/can-i-do-this-syntax-in-julia-when-specifying-types-and-default-values-for-functions/73606/3 "2021-12-25T10:39:15Z")

</div>

I don’t think there is a way to do it. Perhaps a macro could be written to do this. This reminds me of old K&R C style function definition.

I, personally, don’t like this. Typing twice every argument? It seems error prone and verbose.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 25, 2021, 11:14am UTC](https://discourse.julialang.org/t/can-i-do-this-syntax-in-julia-when-specifying-types-and-default-values-for-functions/73606/4 "2021-12-25T11:14:52Z")

</div>

One attempt on this weird thing:

```julia
function f(a, b, c::UInt32 = round(UInt32,a) * round(UInt32,b))
    a = round(UInt32,a)
    b = round(UInt32,b)
    # code ...
    return c
end

c = f(1.8,1.5) # c = 0x00000004 ::UInt32
c = f(1.8,1.5, 0x00000025) # c = 0x00000025 ::UInt32

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [December 25, 2021, 12:03pm UTC](https://discourse.julialang.org/t/can-i-do-this-syntax-in-julia-when-specifying-types-and-default-values-for-functions/73606/5 "2021-12-25T12:03:29Z")

</div>

How are you going to be able to use multiple dispatch with this style? I think it’s going to lead to a very inconsistent coding style, where you have to alternate between putting types in the signature and in this extra block.

Note that this syntax is a workaround for missing features in Matlab.

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [December 25, 2021, 3:27pm UTC](https://discourse.julialang.org/t/can-i-do-this-syntax-in-julia-when-specifying-types-and-default-values-for-functions/73606/6 "2021-12-25T15:27:39Z")

</div>

I think @assert is exactly what I need thanks!

It is basically I want to make an overly user-friendly tool, so it is really nice being able to do it like that.

I will mark as solution for now, since it seems to be what I need, even though it was not what I asked for (atleast directly)

Kind regards

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [December 25, 2021, 3:29pm UTC](https://discourse.julialang.org/t/can-i-do-this-syntax-in-julia-when-specifying-types-and-default-values-for-functions/73606/7 "2021-12-25T15:29:31Z")

</div>

Could you explain the last sentence you wrote i.e. in regards to missing features?

The reason I wanted to do it like that is that I really liked how I could have a lot of function arguments and in a neat manner be able to tell the user what is required. I did not think about multiple dispatch etc.

I think @tbeason suggestion with @assert is what I need when thinking about it in Julia.

Kind regards

---

<div class="post-metadata">

**Author:** ![JanKap](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jankap/32/22763_2.png) [@JanKap](https://discourse.julialang.org/u/JanKap)\
**Post date:** [December 25, 2021, 4:30pm UTC](https://discourse.julialang.org/t/can-i-do-this-syntax-in-julia-when-specifying-types-and-default-values-for-functions/73606/8 "2021-12-25T16:30:46Z")

</div>

This is indeed a workaround for Matlab because your can’t specify the type in the argument list.

But it’s not only about typing, it’s also about default values, validating Inputs, optional (repeating) arguments, and even auto completion. It’s probably the best feature in the recent releases.

Example:

```julia
function myFun(a,b,opts)
arguments
  a double {mustBeScalar} = 1.2
  b string {mustBeMember("choice 1", "choice 2")}
  opts.opt1 = 5
  opts.myopt2
end
end

```

This does a lot. a has default values, b does not. a must be a scalar and will be casted to double if possible. b must be “choice 1” or “choice 2” and is casted to string from compatible types (char, cellstring), these options are auto completed on tab. Opts says there are 2 optional arguments, opt1 and myopt2. One, bothy or none can be provided. And it can be used to assign all properties of a class automatically via reflection.

This is very handy and compact. I really love it.  
@assert helps regarding the validation functions, but it does not handle the auto completed sets in mustBeMember etc.

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [December 25, 2021, 5:10pm UTC](https://discourse.julialang.org/t/can-i-do-this-syntax-in-julia-when-specifying-types-and-default-values-for-functions/73606/9 "2021-12-25T17:10:54Z")

</div>

Exactly, thanks for expressing what I struggled to communicate.

Kind regards

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [December 25, 2021, 10:00pm UTC](https://discourse.julialang.org/t/can-i-do-this-syntax-in-julia-when-specifying-types-and-default-values-for-functions/73606/10 "2021-12-25T22:00:55Z")

</div>

Putting autocompletion aside, as it’s a property of what IDEs do and not the code itself, your `myFun` example can roughly be translated to Julia like this:

```julia
function my_fun(b::String, a::Real=1.2; opt1=5, myopt=nothing)
    @assert b in ("choice 1", "choice 2")
    ...
end

```

Anything missing here?
