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

feat: implement scoped type alias #2806

Merged
merged 1 commit into from
Nov 23, 2023

Conversation

HerrCai0907
Copy link
Member

use Flow to store scoped type alias and lookup it when resolve NamedType.

@MaxGraey
Copy link
Member

MaxGraey commented Nov 22, 2023

Does it support type Elem = valueof<T>? It will be great to reduce verbosity for this method

@HerrCai0907
Copy link
Member Author

HerrCai0907 commented Nov 22, 2023

Does it support type Elem = valueof<T>? It will be great to reduce verbosity for this method

Yes. I will update std after this PR merged
The output of these code would be

function foo<T extends ArrayLike<number>>(): void {
  type Elem = valueof<T>;
  trace(nameof<Elem>());
}

export function _start(): void {
  foo<i32[]>();
  foo<u32[]>();
  foo<string[]>();
}
i32
u32
String

Copy link
Member

@CountBleck CountBleck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beautiful. I'd guess this doesn't work for:

function foo(): void {
    let baz: Bar = []; // Bar needs to be hoisted
    type Bar = Array<i32>;
}

Still, almost no one depends on that detail, so this is practically ready to merge (unless I've missed something).

@CountBleck CountBleck linked an issue Nov 23, 2023 that may be closed by this pull request
@HerrCai0907
Copy link
Member Author

HerrCai0907 commented Nov 23, 2023

Beautiful. I'd guess this doesn't work for:

function foo(): void {
    let baz: Bar = []; // Bar needs to be hoisted
    type Bar = Array<i32>;
}

Yes. It is a huge feature we cannot fulfill at this moment. TS can do definition after usage. Actually I don't like to this design. Maybe we can add something like "scope analysis` to detect and diagnose it.

@CountBleck
Copy link
Member

It's not a big deal. This is good as-is.

@HerrCai0907 HerrCai0907 merged commit 1e0466e into AssemblyScript:main Nov 23, 2023
13 checks passed
@HerrCai0907 HerrCai0907 deleted the type-alias branch November 23, 2023 12:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement inner type aliases
3 participants