Skip to content

Add support for keyword-style arguments #245

Open

Description

@ctrueden

Some programming languages (e.g. Python) have support for keyword arguments, in addition to only ordered arguments. Furthermore, in Python at least, one can have a function with keyword only arguments, which can happen if an argument follows the varargs *args syntax, or in general for the **kwargs syntax. In either situation, the name of the parameter must be given along with the intended value.

SciJava Ops currently does not have a feature like this, but it could. Here is one possible syntax:

Img image = ...;
double sigma = ...;
Img result = ...;
Computers.Arity2<Img, Double, Img> gaussOp =
ops.op("filter.gauss")
    .input("image", image)
    .input("sigma", sigma)
    .output(result)
    .computer();

In the example above, the image and sigma parameters are individually given by separate .input(name, value) calls. A big advantage of this design is that it preserves the type safety offered by Ops. A downside is that the OpBuilder infrastructure would need to expand a lot to accommodate it. Another difficulty is that the op matching engine is currently fully built around the idea of ordered parameters, and it would need refactoring to support op requests with unordered+named inputs. But all of that could certainly be done with effort.

This goal of supporting op matching by named inputs would make it more elegant to wrap such Python functions into Ops. But even with it in place, Ops does not support varargs and might never do so, since it weakens type safety.

Activity

  1. added this to the unscheduled milestone on Jul 29, 2024
  2. gselzer commented on Jul 31, 2024

    @gselzer
    Member

    I appreciate your solution, however for completeness I'll write down another solution - simply treat keyword-only arguments as positional arguments. At the end of the day, we'll need Python-based OpInfo objects that will have to know how to make Ops capable of calling Python code - they could give the impression of required positional arguments that get permuted into keyword-only arguments. The main benefit of this course of action is no additional API, and no changes to the matcher. This may be a good first attempt targeted for minimal effort and refactored later if insufficient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions