google / closure-library

Google's common JavaScript library
code.google.com/closure/library/
Apache License 2.0
4.88k stars 1.04k forks source link

Object type is not inferred from multimap #771

Closed naamaamir closed 1 month ago

naamaamir commented 7 years ago

Hi,

I have this multimap with callback functions. When I compile I get "expressions are not callable" error.

Code example:

/** @private {!Multimap<!string,!function(!Match)>} map  */
this.map = new Multimap();
// ....
for (let callback of this.map.get(key)) {
  callback(match);
}

The get method returns Array<*> instead of Array<!function(!Match)>. I understand that generics should be used. I opened this issue to closure-compiler: https://github.com/google/closure-compiler/issues/2151 But I was redirected to the closure-library.

Thanks!

shicks commented 7 years ago

The main problem in your code is that Multimap<function(!Match)> is nonsense since Multimap is not declared with @template. The compiler should probably have a warning for using template paramaters for non-template types (it was impossible when generics were first added since there were so many spurious/incorrect usages, but now that's it's been there for years, this may be feasible).

But Dimitris is right that the "correct" solution would be to add the @template. This should be possible, but goog.structs and goog.labs.structs are both basically in maintenance mode, since we are working on a cleaner and more coherent collections library. If you want to try to add the generics and properly type the methods, feel free to send a PR.

(Also note that the ! in !string and !function are redundant - the compiler already assumes these lower-case types are non-nullable.)

shicks commented 1 month ago

This issue is being closed as a preparation step before archiving the repository. See the README for more detail.