Skip to content

Custom functions does not support dynamic number of parameters #322

Description

@aurel026362

Built-in functions like xconcat and xadd accept a variable number of arguments. However, when attempting the same with a custom registered function, it fails.

Given a custom function:
public static bool XAnd(object[] arguments) => arguments.All(a => Convert.ToBoolean(a));

Registered and called as:
#xand(true,false,true)

The following error is thrown:

System.ArgumentException: Object of type 'System.String' cannot be converted to type 'System.Object[]'.
   at JUST.ReflectionHelper.InvokeCustomMethod[T](...)

It appears that ReflectionHelper.InvokeCustomMethod passes each comma-separated argument individually via reflection, rather than packing them into the array parameter. This means variadic behavior is only available for built-in functions that presumably have special handling in the parser.
Using params object[] does not help either, as params is a C# compiler feature and is not recognized by MethodBase.Invoke at runtime.

The only way to pass a value to an object[] parameter is by providing a single argument that already resolves to an array (e.g., #valueof($.someArrayPath)). There is no way to pass multiple individual arguments that get collected into an array, which limits the usefulness of custom functions compared to built-in ones.

So, is there a way to achieve variadic behavior with custom registered functions, or would the library need to be extended to support this (e.g., by detecting [ParamArray] in InvokeCustomMethod and packing arguments accordingly)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions