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

preferunits without global state #601

Open
sostock opened this issue Jan 5, 2023 · 1 comment
Open

preferunits without global state #601

sostock opened this issue Jan 5, 2023 · 1 comment
Labels

Comments

@sostock
Copy link
Collaborator

sostock commented Jan 5, 2023

Currently, preferunits works by changing the values in the global dictionary Unitful.promotion. This causes problems with precompilation (#545, #600).

My suggestion for Unitful v2.0 is to replace the global "unit preference dictionary" (Unitful.promotion) by an object that the user creates and explicitly uses to convert to their preferred units. Instead of

preferunits(cm)
upreferred(1m)

the user would do the following:

my_unit_preference = UnitPreference(cm) # this returns a UnitPreference object, does not alter global state
my_unit_preference(1m)                  # the UnitPreference object is callable

However, this means that automatic promotion would not promote to the user’s preferred units but always to the default units (unless they are using ContextUnits/FixedUnits).

Introducing this new UnitPreference object can be done in a minor release, but removing the possibility of setting a global unit preference would be breaking.

@j-fu
Copy link

j-fu commented Jan 5, 2023

My initial feeling says that this is the more sane of the two solutions discussed for #545. Howeverthis seems to shift the temptation to create global state to client packages...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants