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

Use runtime check instead compile time for Array#flat #1353

Merged
merged 4 commits into from
Jun 25, 2020
Merged

Use runtime check instead compile time for Array#flat #1353

merged 4 commits into from
Jun 25, 2020

Conversation

MaxGraey
Copy link
Member

  • I've read the contributing guidelines

@MaxGraey
Copy link
Member Author

MaxGraey commented Jun 24, 2020

Solved most of problems. But found another edge case:

export class ArrayArrayI32 extends Array<Array<i32>> {}

Which print errors:

ERROR TS2469: The '>' operator cannot be applied to type '~lib/array/Array<i32>'.

       return (a: T, b: T): i32 => (i32(a > b) - i32(a < b));
                                        ~~~~~
   in ~lib/util/sort.ts(41,38)

ERROR TS2322: Type '~lib/array/Array<i32>' is not assignable to type 'i32'.

       return (a: T, b: T): i32 => (i32(a > b) - i32(a < b));
                                        ~~~~~
   in ~lib/util/sort.ts(41,38)

...

Which is make sense but I'm wondering could we also handle this case? Perhaps add some helpter like hasOperator<">">()?

std/assembly/array.ts Outdated Show resolved Hide resolved
export class ArrayU8 extends Array<u8> {}
export class ArrayStr extends Array<string> {}
// TODO:
// export class ArrayArrayI32 extends Array<Array<i32>> {}
Copy link
Member

Choose a reason for hiding this comment

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

What happens in this case?

Copy link
Member Author

@MaxGraey MaxGraey Jun 25, 2020

Choose a reason for hiding this comment

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

#1353 (comment)

But it's expected due to Array hasn't comparison operators < and >

Copy link
Member

Choose a reason for hiding this comment

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

I see, hmm. Perhaps if we had a better standard than @operator, like using unique symbols, one could check with isDefined. Looks like this isn't easily solvable right now and requires a follow-up PR anyway, though.

Copy link
Member Author

@MaxGraey MaxGraey Jun 25, 2020

Choose a reason for hiding this comment

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

I guess it could be solved something like this:

function comparator<T>(a : T, b: T): i32 {
   if (hasOperator<T>("<") && hasOperator<T>(">")) {
      return i32(a > b) - i32(a < b);
   } else {
      throw new TypeError("type hasn't comparison operators");
   }
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Looks like this isn't easily solvable right now and requires a follow-up PR anyway, though.

Yes, definitely it's for separate PR

Copy link
Member

Choose a reason for hiding this comment

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

Merged this so we have a fix for the reported use case already. Let's think about improving the other cases in a follow-up :)

@dcodeIO dcodeIO merged commit 40440f7 into AssemblyScript:master Jun 25, 2020
@MaxGraey MaxGraey deleted the fix-1351 branch June 25, 2020 12:19
@github-actions
Copy link

🎉 This PR is included in version 0.12.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

@jtenner
Copy link
Contributor

jtenner commented Jun 26, 2020

Turns out that some compile time checks aren't all that clever :)

@HerrCai0907
Copy link
Member

HerrCai0907 commented Aug 7, 2022

@MaxGraey Do you think it can be reverted now because I have tried in the current compiler. It can pass the compile:

class MyArray extends Array<Array<u8>> {}

export function _start(): void {
  let a = new MyArray();
  a.push([1])
  a.push([2,3]);
  let b = a.flat();
}

@MaxGraey
Copy link
Member Author

MaxGraey commented Aug 7, 2022

Yeah, I guess we could revert this. PR are welcomed

HerrCai0907 added a commit to HerrCai0907/assemblyscript that referenced this pull request Aug 7, 2022
MaxGraey pushed a commit that referenced this pull request Aug 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants