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

Address code review of #5127 #5263

Merged
merged 4 commits into from
Oct 20, 2015
Merged

Address code review of #5127 #5263

merged 4 commits into from
Oct 20, 2015

Conversation

zhengbli
Copy link
Contributor

@vladima
Copy link
Contributor

vladima commented Oct 15, 2015

👍

@@ -82,7 +82,7 @@ namespace ts {
return node.end - node.pos;
}

export function arrayIsEqualTo<T>(arr1: T[], arr2: T[], comparer?: (a: T, b: T) => boolean): boolean {
export function arrayIsEqualTo<T>(arr1: T[], arr2: T[], comparer?: (a: T, b: T) => boolean, sortBeforeComparison = false): boolean {
Copy link
Member

Choose a reason for hiding this comment

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

Where else is this used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@@ -360,7 +360,7 @@ namespace ts {
let newFileNames = ts.map(parsedCommandLine.fileNames, compilerHost.getCanonicalFileName);
let canonicalRootFileNames = ts.map(rootFileNames, compilerHost.getCanonicalFileName);

if (!arrayStructurallyIsEqualTo(newFileNames, canonicalRootFileNames)) {
if (!arrayIsEqualTo(newFileNames.sort(), canonicalRootFileNames.sort())) {
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a comment that if the file names are not the same, we just throw away the old program and start fresh?

Copy link
Contributor

Choose a reason for hiding this comment

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

can newFileNames or canonicalRootFileNames ever be undefined? previously it was handled in arrayIsEqualTo and now it the same situation will lead to crash.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Considering @vladima's comment and the fact that structrual comparison would probably be used in other places later, it might be better to change it back to a generic option inside the arrayIsEqualTo. Only difference is I'll add an optional comparer and don't do the sort in-place.

Copy link
Member

Choose a reason for hiding this comment

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

@zhengbli just do newFileNames && newFileNames.sort(). I don't see why one use case has to conflate the way a core function is written.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If so there will be multiple places to add the null check and comparison (because if newFilenames and canonicalFileNames are both null it should also compare the two), which seems redundant. Plus the added optional parameters don't really have side effects to arrayIsEqualTo.

@DanielRosenwasser
Copy link
Member

Other than my last comment, 👍

let newArray2 = sortBeforeComparison ? array2.slice().sort(comparer) : array2;

for (let i = 0; i < array1.length; ++i) {
let equals = equaler ? equaler(newArray1[i], newArray2[i]) : newArray1[i] === newArray2[i];
Copy link
Member

Choose a reason for hiding this comment

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

You don't need a function to determine if something is equal, comparer should just return 0.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if you don't want to sort before comparing, you don't always have a comparer to use though.

Copy link
Member

Choose a reason for hiding this comment

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

You can (and should) just add an overload.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I imagine you mean:

export function arrayIsEqualTo<T>(array1: T[], array2: T[], sortBeforeComparison?: boolean, comparer?: (a: T, b: T) => number): Boolean;
export function arrayIsEqualTo<T>(array1: T[], array2: T[], equaler?: (a: T, b: T) => boolean): Boolean;

That means if I want to use the sortBeforeCompare option, I must supply a comparer which returns int, even when I don't care about the sorting algorithm. Which also means I cannot use existing functions in our code which already return Boolean. I do not think that is better in terms of compatibility.

Copy link
Member

Choose a reason for hiding this comment

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

Not at all. I mean

export function arrayIsEqualTo<T>(array1: T[], array2: T[], comparer?: (a: T, b: T) => number)): boolean;
export function arrayIsEqualTo<T>(array1: T[], array2: T[], comparer: (a: T, b: T) => number), sortBeforeComparison: boolean): boolean;

zhengbli added a commit that referenced this pull request Oct 20, 2015
@zhengbli zhengbli merged commit 1b36407 into microsoft:master Oct 20, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants