google / google/elemental2

Generate static method for generic methods.

Open
#107 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Java
Stars
170
Forks
41
PR merge metrics
No merged PRs in 30d

Description

Some javascript methods like [Array.prototype.shift](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift#Description) are generics. It means they can be called or applied to objects resembling to the type defining the method.

In Closure-compiler, the @this annotations in function's jsdoc is used for that purpose and for the time being we just skip @this annotation (we even remove them by patching externs file).

We should use this information and create static methods that allow the developer to call those methods with another type of object.

Let's take for example the [Array.prototype.shift definition](https://github.com/google/closure-compiler/blob/edd9274aedc4d76759673b69d043189b68499aca/externs/es3.js#L634), this method can be called on any IArrayLike object. We should generate the following method on JsArray.java class:

```java
@JsType
public class JsArray {
@JsOverlay
public static T shift(IArrayLike thisObject) {
return (T) shift_function.call(thisObject);
}

@JsProperty(name = "Array.prototype.shift", namespace = JsPackage.GLOBAL)
private static Function shift_function;

}
```

Note: the code snippet is just an idea how we could implement the improvement. We should discuss the final implementation (unchecked cast, auto-boxing, introducing a specific Type for shift_function...)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.