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

Always return Quantity, not raw numbers (breaking change) #185

Open
chiphogg opened this issue Oct 29, 2023 · 2 comments
Open

Always return Quantity, not raw numbers (breaking change) #185

chiphogg opened this issue Oct 29, 2023 · 2 comments
Labels
⬇️ affects: code (interfaces) Affects the way end users will interact with the library 📁 kind: enhancement New feature or request 💪 effort: large
Milestone

Comments

@chiphogg
Copy link
Contributor

Right now, the result of a Quantity computation whose unit is equivalent to 1 is a raw number, rather than a Quantity equivalent to unos. I made this decision because people generally expect this kind of computation to produce a raw number. We do provide implicit convertibility to the raw number type, but this can sometimes fail to trigger. Experience shows that the only thing that always acts like a raw numeric type is that type itself.

I believe that further experience has shown this to be a mistake. It makes generic programming too hard. I used to think this was fine, because generic programming is mainly the domain of more experienced users, who will know how to handle this. I was wrong: the reason it's still a problem is that users can't be confident whether they'll get a Quantity or raw number just from inspecting a computation. If we refactor and change the unit of one of the participating quantities, it can even switch from one to the other!

The way to solve this is to provide some idiomatic function --- say, as_raw_number(...) --- which "unpacks" a dimensionless quantity. This helps other (non-unity) dimensionless use cases, too. Alternatively, we could just tell everyone to write .in(unos).

This is a breaking change. The best time to do this would be when we're nearly ready to release 0.4.0. We will also need to use Aurora's AV repo to see how "bad" this change would be in terms of what it does to the code.

@chiphogg chiphogg added 📁 kind: enhancement New feature or request 💪 effort: large ⬇️ affects: code (interfaces) Affects the way end users will interact with the library labels Oct 29, 2023
@chiphogg chiphogg added this to the 0.4.0 milestone Oct 29, 2023
@jaredjxyz
Copy link

+1 for this change... There are certainly tradeoffs, but I'll also add that this could unlock the ability to create more dimensionless units - for example, .in(dozens), .in(bakers_dozens)

@chiphogg
Copy link
Contributor Author

One slight complication to consider is that some users might have functions that are generic not just across different Quantity types, but also between Quantity types and raw numbers (and possibly other types). See #248 for an example.

This means that .in(unos) probably won't work. However, we could design the as_raw_number(...) API so that:

  • it returns its argument for anything other than a Quantity
  • it returns the value in unos for a dimensionless Quantity
  • it produces an error for any other type of Quantity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⬇️ affects: code (interfaces) Affects the way end users will interact with the library 📁 kind: enhancement New feature or request 💪 effort: large
Projects
None yet
Development

No branches or pull requests

2 participants