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?
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
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.
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.
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.
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