allowing a class or any of its subclasses

28 views
Skip to first unread message

John Munro

unread,
May 23, 2013, 6:10:57 PM5/23/13
to closure-lib...@googlegroups.com
I'm trying to pass a couple of constructors to a class so that it can internally create the instances, but the JSDoc notation is causing me problems.

When I try to compile this:

/**
 * Creates an object that encapsulates Closure's Storage and Storage Mechanism classes, basically so that we can call clear.
 *
 * @param {function(new:goog.storage.Storage, !goog.storage.mechanism.Mechanism)} storageType The type of storage to create.
 * @param {function(new:goog.storage.mechanism.Mechanism)} mechanismType The type of storage mechanism to create.
 * @constructor
 */
filevision.storage.Storage = function(storageType, mechanismType) {
    /**
     * The internal storage mechanism
     * @private {goog.storage.mechanism.Mechanism}
     */
    this.mechanism_ = new mechanismType();
 
    /**
     * The internal storage
     * @private {goog.storage.Storage}
     */
    this.storage_ = new storageType(this.mechanism_);
};

 
/**
 * Global property to provide localStorage
 * @const {filevision.storage.Storage}
 */
filevision.storage.Local = new filevision.storage.Storage(goog.storage.CollectableStorage, goog.storage.mechanism.HTML5LocalStorage);

I get this error from the Closure Compiler:

filevision\storage\storage.js:102: WARNING - actual parameter 1 of filevision.storage.Storage does not match formal parameter
found   : function (new:goog.storage.CollectableStorage, goog.storage.mechanism.IterableMechanism): undefined
required: function (new:goog.storage.Storage, goog.storage.mechanism.Mechanism): ?
filevision.storage.Local = new filevision.storage.Storage(goog.storage.CollectableStorage, goog.storage.mechanism.HTML5LocalStorage); //new goog.storage.CollectableStorage(new goog.storage.mechanism.HTML5LocalStorage());

How can I say "goog.storage.Storage or any of its subclasses"?

Nick Santos

unread,
May 23, 2013, 6:16:50 PM5/23/13
to closure-lib...@googlegroups.com
The compiler is correct: Storage wants a function that accepts all
Mechanisms, and you're giving it a function that only accepts
IterableMechanisms.

There is a longer explanation here:
http://closuretools.blogspot.co.uk/2012/02/type-checking-tips.html

TLDR: you want {function(new:goog.storage.Storage, ?)}
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "Closure Library Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to closure-library-d...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Nick Santos

unread,
May 23, 2013, 6:18:15 PM5/23/13
to closure-lib...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages