Static Methods Are Dropped On Externalised Interfaces
See original GitHub issueThe compiler wouldn’t keep static methods even though they are defined in externs.
Externs | Source |
---|---|
|
|
OUTPUT
'use strict';
class b {
api(a) {
console.log(a);
}
static a(a) {
console.error(a);
}
}
module.exports = b;
java -jar google-closure-compiler-java/compiler.jar --compilation_level \
ADVANCED --language_out ECMASCRIPT_2017 --formatting \
PRETTY_PRINT --js_output_file t/c.js
--module_resolution NODE --js \
t/index.js t/externs.js
The title says “dropped”, and if there was no nocollapse
, the static method would have been removed. The workaround is to write
Workaround | Compiled |
---|---|
|
|
OK the static method is not used, but nor is the instance method – they should both be present, unless I understand wrongly what effect the Test.staticMethod
has in externs.
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (11 by maintainers)
Top Results From Across the Web
Eclipse does not allow access to static interface method in ...
The problem was caused by using a pre-Java-8 version in Workspace B. There is no bug in Eclipse or Java, Java 7 and...
Read more >Why were default and static methods added to interfaces in ...
static methods do not belong to interfaces. They belong to utility classes. "default" methods shouldn't have been allowed in interfaces at all. You...
Read more >Suggestion: Add abstract static methods in classes ... - GitHub
The related problem concerns static modifier on interface methods declaration, which is disallowed. 1. The problem. 1.1. Abstract static methods ...
Read more >Are Static Methods/Variables bad practice? - Tom Butler
In short: Yes. There are many disadvantages and static methods should almost never be used. Static methods allow procedural/functional code ...
Read more >Java 8 Interface Changes - static method, default method
Java 8 interface changes include static methods and default methods in interfaces. Prior to Java 8, we could have only method declarations ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@zavr-1 to you
randomObject
defined in externs and thefile
variable created in your example code seem completely unrelated, but the type information on them is very weak from the compiler’s point of view. Basically, they are seen as simple subclasses ofObject
which means its safest to assume they are related to each other.That’s right, good catch - implementing an interface is only about the instance.
@export
may not be what you want, either. I’m not sure if there is way to write what you want. Effectively you need to define an extern’d interface for the static part of the class, and then you’d want to declare the class itself as an instance of that interface. But that could break other parts, so it’s not great.Abstract classes do keep their statics along, but you can’t extend an extern class, since it doesn’t actually exist. Possibly you could fake it with
@extends
but noextends
clause, though that might also cause an error. There’s talk about allowing to “implement” a class (a la TypeScript) but we don’t have anything concrete for that.I would consider the bug here that we just don’t have a way to write this right now. But I’m also not optimistic about being able to make it possible, since it seems like it will require a pretty significant architectural change.