Skip to content

JavaScript Console/TernDef: Consolidate overloaded Java methods via optional parameter syntax #255

@AFaust

Description

@AFaust

This is more a long-term enhancement idea. Looking at the String.substring function in the tern definition for ecmascript, we can see that optional function parameters are typically documented via a question mark after their name. We could try to conslidate overloaded Java methods for script API extensions to use this and not require separate numbered definitions (where the number suffix has to be removed after completion or the function cannot be resolved/called).

I.e. currently our tern definitions would include the following for ScriptNode:

"addAspect" : {
    "!type" : "fn(type: JavaString) -> bool"
},
"addAspect2" : {
    "!original" : "addAspect",
    "!type" : "fn(type: JavaString, props: object) -> bool"
}

The !original meta field is not actually defined/used by tern. This example could be consolidated into

"addAspect" : {
    "!type" : "fn(type: JavaString, props?: object) -> bool"
}

In some instances, consolidation of overloaded methods might "suggest" to a casual user that an arbitrary combination of parameters may be provided, e.g. if three parameters are optional then the method/function may be called with either none, one, two, or all three parameters present. Since on the Java layer, the method may only exist in overloaded variants for no, one, and three parameters, a naive user may run into an error if they provide exactly two parameters.

In other instances, when the order of parameters in overloaded methods varies greatly, consolidation may not be possible. We should only consolidate those variants of methods which have the same base signature and - if more than two overloaded variants exist - can be clearly identified as having only a redaction/extension of parameters compared to all other variants. E.g. for ScriptNode.createNode the following variants exist:

public ScriptNode createNode(String name, String type)
public ScriptNode createNode(String name, String type, String assocType)
public ScriptNode createNode(String name, String type, Object properties)
public ScriptNode createNode(String name, String type, Object properties, String assocType)
public ScriptNode createNode(String name, String type, Object properties, String assocType, String assocName)

Due to the existence of the (String, String, String) variant, this overloaded method cannot be fully consolidated. An attempt should be made to consolidate as many overloaded variants as possible, and when incompatible variants exist to exposse them with number-suffixed alternative signatures - just as we are currently doing. For ScriptNode.createNode this would result in something like the following:

"createNode" : {
    "!type" : "fn(name: JavaString, type: JavaString, properties?: object, assocType?: JavaString, assocName?: JavaString) -> ScriptNode"
},
"createNode2" : {
    "!original": "createNode",
    "!type" : "fn(name: JavaString, type: JavaString, assocType: JavaString) -> ScriptNode"
}

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions