Skip to content

Object literals #633

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

Closed
trusktr opened this issue Jun 6, 2019 · 7 comments
Closed

Object literals #633

trusktr opened this issue Jun 6, 2019 · 7 comments
Labels

Comments

@trusktr
Copy link
Member

trusktr commented Jun 6, 2019

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.

@dcodeIO
Copy link
Member

dcodeIO commented Jun 6, 2019

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.

@trusktr
Copy link
Member Author

trusktr commented Jun 6, 2019

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?

@trusktr
Copy link
Member Author

trusktr commented Jun 6, 2019

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

@trusktr
Copy link
Member Author

trusktr commented Jun 6, 2019

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

@trusktr
Copy link
Member Author

trusktr commented Jun 6, 2019

Oh, duh, Map#has. nvm

@lastmjs
Copy link

lastmjs commented Dec 31, 2019

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

@MaxGraey
Copy link
Member

MaxGraey commented Dec 31, 2019

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

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

No branches or pull requests

4 participants