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 ??= over contains and ! for map defaulting #3241

Merged
merged 1 commit into from Feb 1, 2022

Conversation

natebosch
Copy link
Member

Avoid a non-null assertion by chaining access from a null conditional
assignment instead of separately inserting to the map, then reading it
back out for further operations.

Also refactor an iterable .toList() to a List literal.

Avoid a non-null assertion by chaining access from a null conditional
assignment instead of separately inserting to the map, then reading it
back out for further operations.

Also refactor an iterable `.toList()` to a List literal.
@natebosch
Copy link
Member Author

I noticed some stuff I could code-golf when I was trying to understand behavior, and after looking at it I think this is actually nicer despite being a bit dense.

librariesByDirectory[dir] = <AssetId, ModuleLibrary>{};
}
librariesByDirectory[dir]![library.id] = library;
(librariesByDirectory[dir] ??= {})[library.id] = library;
Copy link
Contributor

Choose a reason for hiding this comment

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

we could use putIfAbsent also, but 🤷‍♂️

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I had thought about that pattern too but for some reason ??= looked nicer to me.

Maybe it was something like dart2js emitting better code for ??= than putIfAbsent? (even though this will not be compiled with dart2js)

Copy link
Contributor

Choose a reason for hiding this comment

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

I would usually use putIfAbsent, it's true that this doesn't require a closure though. I am fine with either way.

@natebosch natebosch merged commit 30df213 into master Feb 1, 2022
@natebosch natebosch deleted the null-equals-over-contains branch February 1, 2022 01:07
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.

None yet

2 participants