Generics error

Topics: General
Jul 31, 2013 at 11:00 PM
We have a cache data structure which has a method which looks like this:
    public get(key: K, valueLoader?: () => V): V {
        var value = this._cache.get(key);

        // load and save the value if one was not found in the _cache
        if (value === undefined && valueLoader !== undefined) {
            value = valueLoader();
            this.set(key, value);
        }

        return value;
    }
This compiled in 0.9.0.1, but with the in-progress 0.9.1 compiler I'm seeing the following error on the valueLoader parameter's type: "Return type of exported function is using inaccessible module".

In case it matters, the context for this code looks like this (we compile to AMD modules):
import Map = require("common/map");

class Cache<K, V> {

    private _cache: Map<K, V>;
    private _maxSize: number;

    constructor(maxSize?: number, lru = false) {
        if (maxSize !== undefined && maxSize < 1) {
            throw new Error("maxSize must be positive.");
        }

        this._cache = new Map(lru);
        this._maxSize = maxSize;
    }

...

}
export = Cache;
Is this error message expected? I can provide the entire file and its dependency (map.ts) if necessary. Thanks for any assistance!
Aug 5, 2013 at 10:16 PM
ping - i see there is another thread on generics but i'm not sure if this one is related.
Aug 6, 2013 at 10:46 AM
Edited Aug 6, 2013 at 10:53 AM
Can you try
get(key: K, valueLoader?: { (): V; }): V
instead of
get(key: K, valueLoader?: () => V): V
as the method signature?
Aug 6, 2013 at 4:44 PM
Whoa, good call: that works fine. Do you know why that works but the other syntax doesn't? Is it a bug in the compiler? (or a breaking change in 0.9.1)
Aug 6, 2013 at 7:52 PM
One of my colleagues found another fix which is the one I think we'll go with:
public get(key: K, valueLoader?: () => Cache.V): V {
Aug 7, 2013 at 10:08 AM
Edited Aug 7, 2013 at 10:09 AM
I find that doing it via an interface works:
interface FuncReturning<V> { (): V; }
public get(key: K, valueLoader?: FuncReturning<V>): V { ... }
This must be a bug in 0.9.1 since it didn't show up in 0.9.01.