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

Hi!

The newest version of Matlab (Function Argument Validation - MATLAB & Simulink - MathWorks Nordic) has this capability:

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

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

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.

2 Likes

One attempt on this weird thing:

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
2 Likes

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.

3 Likes

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

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

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:

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.

2 Likes

Exactly, thanks for expressing what I struggled to communicate.

Kind regards

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:

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

Anything missing here?

3 Likes