GithubHelp home page GithubHelp logo

value() and value_type about units HOT 9 CLOSED

JaapAap avatar JaapAap commented on July 16, 2024
value() and value_type

from units.

Comments (9)

nholthaus avatar nholthaus commented on July 16, 2024 3

In some ways both terms are lacking.

value() implies that a unit consists of two separate parts, a container and a value which is contained. This is sort of the opposite of how I see units; in my mind units are trivial types, not containers. 3_m is the value. Someday I want people to think of raw arithmetic types the same way they do for raw pointers, a.k.a. distasteful and best avoided (all things being equal). When that happens, value starts to make less sense.

magnitude() kind of makes sense (more so in boost/physunits which also have the quantity syntax), but usually magnitude in physics implies no dimension or direction, so does it really make sense if the magnitude() returned a negative value? It does have implications of being a mathematical transform, which I like.

operator() is probably the worst, because of how often it requires a double-parenthesis ()() syntax, and that it gives no context. The only merit is brevity.

unit_cast<...>() to me is the most standard-ese. It's an explicit cast for a potentially unsafe operation, and it's reminiscent of std::chrono::duration_cast<...>(). It makes it clear that you are losing something, and implies to readers (correctly) that something kludegy was happening for some reason. to<..>() is basically just unit_cast in member-function form.

Don't take these musings to mean that I've made a decision.

As far as standardization goes, I'd prefer the committee was making these types of calls instead of myself, but naming conventions are easily changed in the future std fork so I'm not too worry about making the "wrong" decision in v2.3.0.

from units.

nholthaus avatar nholthaus commented on July 16, 2024

easy enough. I'll think about it tonight and if there are no unintended consequences could roll it into a v2.2.1.

You may not want it to be so easy to carry around .value() though. Unless it's an interface boundary, I suspect unit_t<...> can be used without conversion to arithmetic types in many more places than boost::units.

I'm assuming that you're also already familiar with the fact that the underlying type can be accessed with operator(), as well as the to<..>() function, which will cast the value to any desired arithmetic type.

from units.

martinmoene avatar martinmoene commented on July 16, 2024

and if at all add it: s/value()/magnitude()/ ?

from units.

nholthaus avatar nholthaus commented on July 16, 2024

@martinmoene I think unless it matched the boost syntax exactly then there isn't any point? Since you can already get to the underlying value using:

  • operator() (member)
  • unit_cast<...>() (static)
  • to<...>() (member)

from units.

JaapAap avatar JaapAap commented on July 16, 2024

from units.

martinmoene avatar martinmoene commented on July 16, 2024

Instead of thinking about backwards compatibility, I was thinking forward about standardization. In case one explicitly would want to use a quantity's magnitude, magnitude() might better express that intent than value() does. Just to have it mentioned.

from units.

JaapAap avatar JaapAap commented on July 16, 2024

from units.

JaapAap avatar JaapAap commented on July 16, 2024

from units.

nholthaus avatar nholthaus commented on July 16, 2024

I'm going to put this into the next version. I think overall it does no harm. Whether it's carried forward into a standardization proposal is another matter (and my recommendation would be that it isn't for the reasons already given; in fact I'd strip out all accessors aside from unit_cast).

from units.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.