Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to export a map from a namespace? #2763

Open
BVBAccelm opened this issue Oct 10, 2023 · 3 comments
Open

How to export a map from a namespace? #2763

BVBAccelm opened this issue Oct 10, 2023 · 3 comments

Comments

@BVBAccelm
Copy link

Question

I have a set of data in key-value pairs that is constant, never changing. I need to access the values by using a variable that holds the key (it's dynamic). It is specific to a subset of code, and I'd like to put it in a namespace that relates to it. I figured that creating a Map was the best way to handle this type of data so I can access the values based off of the key (I've had other issues trying to access values on objects via dynamic keys in AssemblyScript otherwise I would use that). I figured exporting it from the namespace would provide me with what I've needed. I've exported functions from the same namespace without any issues. I am open to other options so if there's an AssemblyScript way to handle this please let me know.

The issue that I am having is that all of the code that I have tried exports an empty map, or doesn't compile. The only way to get it to work is to create a function that returns it, and export that function. I'd like to avoid doing this.

Here is some example code:

namespace TestNamespaceOne {
    export const map: Map<string, string> = new Map()
    map.set('key1', 'value1');
}

namespace TestNamespaceTwo {
    const testMap: Map<string, string> = new Map()
    testMap.set('key1', 'value1');
    export const map = testMap;
}

namespace TestNamespaceThree {
    const testMap: Map<string, string> = new Map()
    testMap.set('key1', 'value1');
    export function getTestMap(): Map<string, string>{
        return testMap;
    }
}

namespace TestNamespaceFour {
    export function getTestMap(): Map<string, string>{
        const testMap: Map<string, string> = new Map()
        testMap.set('key1', 'value1');
        return testMap;
    }
}

// namespace TestNamespaceOther {
//     function getMap(): Map<string, string>{
//         const testMap: Map<string, string> = new Map()
//         testMap.set('key1', 'value1');
//         return testMap;
//     }
//     export const map = getMap();
// }

class TestClass {
    constructor(){
        console.log('TestNamespaceOne: '+TestNamespaceOne.map.keys().join(","))
        console.log('TestNamespaceTwo: '+TestNamespaceTwo.map.keys().join(","))
        console.log('TestNamespaceThree: '+TestNamespaceThree.getTestMap().keys().join(","))
        console.log('TestNamespaceFour: '+TestNamespaceFour.getTestMap().keys().join(","))
    }
}

export function testFunction(): void{
    const testClass = new TestClass();
}

When in instantiate TestClass, the following shows up in the console:

TestNamespaceOne: 
TestNamespaceTwo: 
TestNamespaceThree: 
TestNamespaceFour: key1

As you can see, the first three solutions all return an empty map.

I also have TestNamespaceOther in the code that doesn't even compile. When left uncommented, and I try to compile it, I get the following result:

ERROR TS2304: Cannot find name 'getMap'.
      :
 4900 │ export const map = getMap();
      │                    ~~~~~~
      └─ in assembly/index.ts(4900,24)

FAILURE 1 compile error(s)

Somehow it cannot find the function declared immediately before it. I've tried other variations on this with no desired results as well.

The version of AssemblyScript that I am using is 0.25.2.

Any ideas how I can accomplish this? I am not sure if it's a bug or if this is an intended consequence of the way AssemblyScript works. As I've said, I'm open to other options, but being unable to use dynamic properties on objects I figured a map would be the way to go - but if there's a better way to model and access the data then I would gladly use that. just let me know.

@CountBleck
Copy link
Member

Does updating to the latest version have any effect?

@HerrCai0907
Copy link
Member

TestNamespaceOther is definitely bug. I will take a look.

If you want to initialize a map, elegant solution is creating a class to wrapper map and do something in constructor.

@BVBAccelm
Copy link
Author

@CountBleck I updated to 0.27.13 and all of the things that I explained above executed the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants