Assemblyscript: Object literals

Created on 6 Jun 2019  路  7Comments  路  Source: AssemblyScript/assemblyscript

Are those (or will those) be supported?

I didn't have any luck trying to make an object literal, so I'm using Map. I didn't see any mention of object literals in the new docs.

question

All 7 comments

There is a mention at https://docs.assemblyscript.org/basics#strict-typing

Objects must be strictly typed as well:

// 馃槩
var a = {};
a.prop = "hello world";

// 馃槉
var a = new Map<string,string>();
a.set("prop", "hello world");

// 馃槒
class A {
  constructor(public prop: string) {}
}
var a = new A("hello. world");

What this doesn't mention is that there is some support currently, for example {} can be used to initialize an instance of a class in a matching type context, if the constructor has zero arguments. But that's not great yet, so not mentioned.

Ah cool. Thanks. I'm trying to use Map to convert some JS object-literal code over, and the JS code that does something like

        if (listeners[eventName] === undefined) {
            listeners[eventName] = []
        }

So I'm wondering what to do. In the docs:

a get with a key that does not exist results in an error, because undefined cannot be represented.

So a get will crash the program.

I guess I need to go through the code base, and make a list of all possible string keys, and initialize them all?

Hmmm, maybe I need to use an array of array tuples, and findIndex?

Oh, but I don't think Array can support multiple item types?

Oh, duh, Map#has. nvm

I would love to see object literal support. Is that something that will be possible in the future? If so, can we reopen this?

We have some plans. See this for example: #338 and #299. But even now you could do something like this:
https://github.com/ycw/Babylon.Font/blob/master/src_wasm/assembly/index.ts#L477
https://github.com/ycw/Babylon.Font/blob/master/src_wasm/assembly/index.ts#L568

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jerrywdlee picture jerrywdlee  路  4Comments

jarble picture jarble  路  3Comments

andy-hanson picture andy-hanson  路  4Comments

MaxGraey picture MaxGraey  路  4Comments

Iainmon picture Iainmon  路  3Comments