New language feature "arguments" introduced in Scilab 2024.0.0

Do you know the new language feature arguments introduced in Scilab 2024.0.0?

The aim is to simplify the writing of input arguments validation, standardize messages and add readability of code by avoid blocks of checkers like:

if type(a) <> 1 | size(a) <> [2 3] then error("...");end

and potential speedup for function with lot of checks.

This new block takes this generic form

image

and previous example can be replaced by:

function test(a)
    arguments
        a (2, 3) {mustBeA(a, "double")}
    end
endfunction

For each input parameter, Scilab will be check dimensions compatibility, conversion capability and each validator (all must be verified) and if the input parameter is missing it will assign to default value.

All input parameters must be defined in the arguments block. And no check is mandatory, so if you want to check nothing, just let a line with the variable name

function test(a)
    arguments
        a
    end

    //algorithm
endfunction

test(1);

Dimensions

Here are a examples of dimension validation:

function test(a, b)
    arguments
        a (2, 3)
        b (1, 3)
    end

    //algorithm
endfunction

test(rand(2, 3), rand(1, 3));   //works
test(rand(2, 3), rand(3, 1));   //works, b will be transpose to match expected dimensions
test(0, 1);                     //works, a and b will be expanded to match expected dimensions
test(rand(3, 3), rand(2, 2));   //fails
function test(a)
    arguments
        //waits a 2 or 4 columns matrix
        a (:, [2 4])
    end

    //algorithm
endfunction

test(rand(1024, 2)); //works
test(rand(256, 3)); //fails
test(rand(512, 4)); //works

Conversions

Argument checking also manages automatic type conversion as follows:

function test(a)
    arguments
        a double
    end

    disp(type(a))
    //algorithm
endfunction

test(1);         // a is a double
test(uint8(42)); // a will be converted to a double

Validators

And here is how to force type checking:

function test(a)
    arguments
        a {mustBeA(a, "double")}
    end

    //algorithm
endfunction

test(1);         // works
test(uint8(42)); // fails

See the list of all validators

Default Value

If you want to set a default value for input argument, just use the following:

function test(a)
    arguments
        a = 12
    end

    //algorithm
endfunction

test(1);    // a == 1
test();     // a == 12

Since Scilab 2024.0.0, this new feature is already used in time module (see related merge request) and other modules will be updated in next versions…

As usual, don’t hesitate to share with us your use cases and issues.

1 Like

Hi Antoine,
thank you for this nice new feature that I’m introducing in some developments. Would you give some hints, recommendations and examples how to deal with varargin argument ?

David

Hello,
I have a function that take a matrix of the form ( : , : ) or ( : , : ,3). It’s representing a grayscale image or a RGB image. How can I make the “arguments” check this type of argument ?

Hi all,

while working with experimental data in real-time applications, I often need to check the validity range of the input arguments values to detect random measurement errors, either yielding %nan or valid numerical but extrem values,etc… which I need to manage following use-case specific rules/functions to avoid stopping the real-time application : e.g. some default values to be taken instead and yielding a message to record the issue , not necessarily breaking the execution of the program : Do you have an suggestion how I could use the new arguments function to handle smarter such situations ?
Thank you,

David

Hello,

Welcome on Scilab Community !

Normally, using (:, :, [1 3]) for dimensions should work.
But I just checked and that’s not the case.
So I opened the issue #17269 (and proposed a patch for 2025.0)