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

Unions of References and TypedArrays #828

Closed
willemneal opened this issue Sep 8, 2019 · 7 comments
Closed

Unions of References and TypedArrays #828

willemneal opened this issue Sep 8, 2019 · 7 comments
Labels

Comments

@willemneal
Copy link
Contributor

The following doesn't compile

class Foo {}
function fooOrUint8Array(foo: Foo | Uint8Array): void {}

with the following error:

ERROR TS1005: 'null' expected.

 function fooOrUint8Array(foo: Foo | Uint8Array): void {}
                                    ^
 in assembly/main.ts(27,35)

Since Uint8Array has a class id, shouldn't this work?

Since WebAssembly.studio is down here is an example with near's fork:

https://studio.nearprotocol.com/?f=1cg9abhe2

@dcodeIO
Copy link
Member

dcodeIO commented Sep 10, 2019

Wondering a bit, since union types are not supported. What am I missing?

@willemneal
Copy link
Contributor Author

Instead of any, we can use a union type for the padding parameter:

/**
 * Takes a string and adds "padding" to the left.
 * If 'padding' is a string, then 'padding' is appended to the left side.
 * If 'padding' is a number, then that number of spaces is added to the left side.
 */
function padLeft(value: string, padding: string | number) {
    // ...
}

let indentedString = padLeft("Hello world", true); // errors during compilation

A union type describes a value that can be one of several types. We use the vertical bar (|) to separate each type, so number | string | boolean is the type of a value that can be a number, a string, or a boolean.

Were you thinking intersection types?

@DanielRX
Copy link

@willemneal that's typescript, not assemblyscript? Assembly script doesn't support unions as far as I've seen and tried?

@willemneal
Copy link
Contributor Author

Ah yeah I had forgotten. Had seen a lot of | null like this: https://webassembly.studio/?f=yaok3afximh

And had forgotten. I managed to get around with generics like this: https://webassembly.studio/?f=nw6ffxidsvr

Perhaps we can add this to the discussion of #816 since with better type resolution this should be possible.

@stale
Copy link

stale bot commented Oct 11, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Oct 11, 2019
@stale stale bot closed this as completed Oct 18, 2019
@trusktr
Copy link
Member

trusktr commented Nov 21, 2020

Aren't issues like these good to keep open? I wonder if perhaps Google and other search engines might de-prioritize closed issues. Also searching for open issues (the default) won't show results. I may have then not created #1548 if I had found this one first. I thought I searched thoroughly before posting (guess I didn't!).

@dcodeIO
Copy link
Member

dcodeIO commented Feb 17, 2021

To anyone coming here for a status update, a more detailed overview of the current status has meanwhile been added to the AS website. No updates on support for union types so far, though.

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