Skip to content

Conversation

@goerz
Copy link
Collaborator

@goerz goerz commented Jun 15, 2025

The default dict_inverse does not work for ImmutableDict, because ImmutableDict does not implement a ImmutableDict{K,V}(pairs...) constructor.

Closes #43

The default `dict_inverse` does not work for `ImmutableDict`, because
`ImmutableDict` does not implement a `ImmutableDict{K,V}(pairs...)`
constructor.
@codecov
Copy link

codecov bot commented Jun 15, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 86.90%. Comparing base (ed40d49) to head (fecc194).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #44      +/-   ##
==========================================
+ Coverage   85.89%   86.90%   +1.00%     
==========================================
  Files           1        1              
  Lines          78       84       +6     
==========================================
+ Hits           67       73       +6     
  Misses         11       11              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

function dict_inverse(d::D) where {D<:AbstractDict}
allunique(values(d)) || throw(ArgumentError("dict is not bijective"))
return inverse_dict_type(D)(reverse.(collect(d)))
return inverse_dict_type(D)(reverse.(collect(d))...)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The Dict([pairs]) constructor works, but is not documented. The two official constructors are Dict([pair_tuples]) and Dict(pairs...). It seems safer to add the ..., since I would expect more AbstractDict subtype to implement this, over the undocumented version.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The problem with the ... is that it runs into a stack overflow for large number of outputs, so I would avoid in this case where the number of entries is unbounded.

This is the fallback method so it's ok to fail. If that's the case, we add the specialized method like you are doing here with ImmutableDict.

@goerz goerz requested a review from mofeing June 15, 2025 16:13
@mofeing mofeing merged commit 8337376 into master Jun 16, 2025
8 checks passed
@mofeing mofeing deleted the mg/43-immutable-dict branch June 16, 2025 06:44
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.

Cannot instantiate from ImmutableDict

3 participants