Skip to content

Allow primary parameters to be optional #14

Closed

Description

@gselzer

There are good reasons for the removal of secondary parameters in this iteration of Ops

  • Lambda Ops cannot support secondary parameters
  • The current state of OpService.run() cannot currently support secondary parameters and would be slowed down if we were to provide support through that method
  • Removing secondary parameters will result in fewer lines of code

For these reasons it would be best to push towards the exclusive use of primary parameters. However this has one main drawback in that secondary parameters are associated with optional parameters: in the previous iteration of Ops primary parameters were never optional and should never be optional. The current strategy in solving this has been to:

  • Port the Op and make the optional parameter required.
  • Make a second lambda Op that just calls the main Op with a reasonable default for the optional parameter.

In many cases however this severely bloats the code base. In many cases, especially when the Ops are lambdas, this is not a big deal, however it can be problematic when there are multiple optional parameters and/or the Ops must be classes. For this reason it would be nice to allow primary optional parameters in such situations.

As of now the @Parameter annotation can be designated as required = false, however it is currently really only a placeholder. A current strategy is to designate optional parameters as required = false under the contract that the user will pass null for that parameter if they want the reasonable default. Then in the body of the op the developer would check that param:

if(param == null) param = reasonable_default;

This, however, feels lacking. It would be nice if the Op matcher was intelligent enough to be able to read those required = false annotations and be able to match a smaller-order function when the user so desires (i.e. if the user wants to match a Function3 with one optional parameter they can do so by asking the matcher for a BiFunction with the same name and same typing as the required params of that function). This could possibly be done with an OpTransformer.

Activity

  1. ctrueden commented on May 15, 2019

    @ctrueden
    Member

    @Treiblesschorle If you have time: do you have thoughts on the matching of optional parameters with the SciJava Ops matcher? Do you remember if you attempted it? Just punted on it completely? Do you think the OpTransformer mechanism is flexible enough to support matching smaller-order functions? Or would this feature need to be baked into the matcher's core? Or is there already, and we are somehow missing it?

  2. gselzer commented on May 15, 2019

    @gselzer
    MemberAuthor

    @ctrueden I just found this which suggests that optional secondary parameters are supported. I don't have the time left today to dig into this but will attempt to do so on Friday to figure out where this happens / if we can use this as a base for the work that we want to do (maybe @Treiblesschorle will have the time to provide more info in the meantime).

  3. Treiblesschorle commented on May 16, 2019

    @Treiblesschorle
    Collaborator

    Unfortunately, the example mentioned by @gselzer is about not having to specify secondary args that are not required when calling ops().findOp(). These will be set null and the op needs to check for that. However, the types still have to be specified for matching (all primary and secondary ones). At the moment you can't ask for a smaller-order function if one of the params is optional.

    I think it could be done with an OpTransformer if we add these smaller-oder functions as meta ops to the system.

    Maybe we should have a discussion about that in general.

  4. self-assigned this
    on Jun 10, 2019
  5. wiedenm commented on Jun 28, 2019

    @wiedenm
    Member

    We realized that OpTransformers are not a good fit to implement this feature. That's because such transformers would need to know about the optional parameters of their source Ops. This information can only be provided by the OpInfos of actual existing Ops, not by already transformed Ops. This conflicts with the idea of OpTransformers being able to transform any suitable Op only given the source Op object itself and the target OpRef.

    instead, we could extend the Op discovery mechanism a bit, such that Ops that have optional parameters are added multiple times to the index: once in their "full form", and once per omitted set of optional parameters (right-to-left elimination only), as smaller-order functions. They will then get matched as any other "regular" Op.

  6. removed their assignment
    on Jun 28, 2019
  7. gselzer commented on Aug 20, 2019

    @gselzer
    MemberAuthor

    @ctrueden proposed the following tentative scheme:

    When an op declares a parameter as optional, register another OpInfo with that parameter missing (dropping them from right to left in the case of multiple optional parameters)

    Which we then realized is what @MarcelWiedenmann said 😁

  8. wiedenm commented on Aug 26, 2019

    @wiedenm
    Member

    I like that we agree on this 😄.

  9. added this to the 1.0.0 milestone on Jul 7, 2020
  10. ctrueden commented on Jul 7, 2020

    @ctrueden
    Member

    How we can support succinct declaration of optional primary parameters

    @Target(ElementType.PARAMETER)
    public @interface Optional {
    	// Marker interface.
    }
    
    @Plugin(type = Op.class, name = "math.munch")
    public class NumberMuncher implements Function<Double, Integer> {
    
    	@Override
    	public Integer apply(@Optional Double t) {
    		// TODO Auto-generated method stub
    		return null;
    	}
    
    }
    
    @Plugin(type = OpCollection.class)
    public class NumberOps {
    	@OpField(name = "math.munch")
    	private final Function<Integer, Float> widget = (@Optional Integer i) -> i + 0.5f;
    }

    How we can support matching them

    Just add additional OpInfo objects at discovery, which delegate to the original with null padding, from right to left.

    Should we support adapting or any fanciness?

    No. The op implementor is responsible for handling null input for each @Optional parameter they have declared.

  11. gselzer commented on Jul 20, 2020

    @gselzer
    MemberAuthor

    How we can support matching them

    Just add additional OpInfo objects at discovery, which delegate to the original with null padding, from right to left.

    How do we want to determine what functional type the Op without the optional parameter will be? Is there a way to avoid hardcoding the types and preserve extensibility?

  12. gselzer commented on May 13, 2021

    @gselzer
    MemberAuthor

    Time commitment: 1 week

    (possibly more, depending on the complexity of the solution to this)

    How do we want to determine what functional type the Op without the optional parameter will be? Is there a way to avoid hardcoding the types and preserve extensibility?

  13. self-assigned this
    on May 24, 2021
  14. added a commit that references this issue on Oct 31, 2022
    8ac3b05
  15. added a commit that references this issue on Nov 8, 2022
    def32d9
  16. added a commit that references this issue on Nov 14, 2022
    a274309
  17. added a commit that references this issue on May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions