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

Allow .as<Rep>() syntax on a Quantity #256

Open
hoffbrinkle opened this issue Jul 4, 2024 · 1 comment
Open

Allow .as<Rep>() syntax on a Quantity #256

hoffbrinkle opened this issue Jul 4, 2024 · 1 comment
Labels
⬇️ affects: code (interfaces) Affects the way end users will interact with the library 📁 kind: enhancement New feature or request 💪 effort: medium

Comments

@hoffbrinkle
Copy link
Contributor

With the introduction of Constant, there was an API introduced for those that allows the Rep to be indicated without changing the Unit of the constant. Specifically, one can use .as<Rep>() to return a Quantity with the desired Rep from a Constant.

I find this to be a natural API, and often find myself trying to do the same thing on a Quantity when I want a Quantity with a specific Rep. We have several other options when this needs to be done:

  1. .coerce_as<Rep>(Unit)
  2. .as<Rep>(Unit)
  3. rep_cast<Rep>(Quantity)

The first one is scary. The second option works, but forces me know the Unit even if I don't want the Unit to change. The third option is does a static_cast on the underlying value. I think the third option is best suited for my use cases.

What I'm thinking is an API that is .as<Rep>() if I don't want the unit to change, with as much of the "don't accidentally shoot yourself in the foot" checking as the library can provide.

@chiphogg chiphogg added 📁 kind: enhancement New feature or request 💪 effort: medium ⬇️ affects: code (interfaces) Affects the way end users will interact with the library labels Jul 6, 2024
@chiphogg
Copy link
Contributor

chiphogg commented Jul 6, 2024

Fascinating! I strove to make Au's interfaces a kind of "composable grammar", so people could learn the meaning of words like as and in and apply them consistently (e.g., round_as vs. round_in). This implication hadn't occurred to me, but it makes sense.

It also suggests that q.coerce_as<R>() might be a good synonym for rep_cast<R>(q). If we add q.as<R>(), it seems safe to say we should add the coerce version too. I'm less sure about whether we would want to remove rep_cast once we do.

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: medium
Projects
None yet
Development

No branches or pull requests

2 participants