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

remove conflicting default call in tables.getOrDefault #24265

Merged
merged 5 commits into from
Oct 9, 2024

Conversation

metagn
Copy link
Collaborator

@metagn metagn commented Oct 8, 2024

fixes #23587

As explained in the issue, getOrDefault has a parameter named default that can be a proc after generic instantiation. But the parameter having a proc type overrides all other overloads including the magic system.default overload and causes a compile error if the proc doesn't match the normal use of default. To fix this, the result = default(B) initializer call is removed because it's not needed, result is always set in getOrDefaultImpl when a default value is provided.

This is still a suspicious behavior of the compiler but tables working has a higher priority.

@metagn metagn changed the title remove overriden default call in tables.getOrDefault remove conflicting default call in tables.getOrDefault Oct 8, 2024
@@ -434,7 +434,6 @@ proc getOrDefault*[A, B](t: Table[A, B], key: A, default: B): B =
let a = {'a': 5, 'b': 9}.toTable
doAssert a.getOrDefault('a', 99) == 5
doAssert a.getOrDefault('z', 99) == 99
result = default(B)
Copy link
Member

Choose a reason for hiding this comment

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

No. Instead the parameter default: B should be def: B.

Copy link
Collaborator Author

@metagn metagn Oct 9, 2024

Choose a reason for hiding this comment

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

Did both, and did it for all parameters named default in tables (OrderedTable had the same bug). There are others across the codebase but they're less harmful and this PR being simpler is better.

Unfortunately this breaks named argument usage (i.e. getOrDefault(foo, bar, default = val)). We can change the code in this repo but it might still break packages.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah I know but it's a risk I'm willing to take.

metagn added a commit to metagn/Nim that referenced this pull request Oct 9, 2024
@Araq Araq merged commit 67ea754 into nim-lang:devel Oct 9, 2024
19 checks passed
Copy link
Contributor

github-actions bot commented Oct 9, 2024

Thanks for your hard work on this PR!
The lines below are statistics of the Nim compiler built from 67ea754

Hint: mm: orc; opt: speed; options: -d:release
175046 lines; 8.238s; 655.223MiB peakmem

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.

Error: type mismatch: got <typedesc[A]>, but expected one of: A = proc (){.closure.}
2 participants