Git Product home page Git Product logo

Comments (2)

AStepaniuk avatar AStepaniuk commented on June 12, 2024

What units formatting approach is supposed to be?

A few different ones could be considered:

  1. Simple one: Put only the quantity (i.e. value from unit.count()) into formatted string.
    I.e.

    std::string s = fmt::format("The speed is {} kmph", 2_kmph);
    // s == "The speed is 2 kmph"
    

    Which is simple in use and appears to be easy in implementation.
    The obvious drawback here is that the information about dimensions is not used in formatting.
    So, some useful information becomes unavailable in formatting string and it becomes possible to make a mistake in formatting string - write wrong dimensions.

  2. More complex one: Give ability to refer to both quantity and dimensions from formating string.
    In that case special format specification mini language will be required.
    Probably, something similar to Date and Time formatting could be taken.

    For example, like that:

    std::string s = fmt::format("The speed is {:%q %d}", 2_kmph);
    // s == "The speed is 2 kmph"
    

    Or it could be:

    std::string s = fmt::format("The speed, {0:%d}: {0:%q}", 2_kmph);
    // s == "The speed, kmph: 2"
    

    Where %q refers to quantity, and %d refers to dimensions.

    I think, it might be also possible to make it compliant with standard fmt features. Though the syntax becomes complicated:

    std::string s = fmt::format("The speed is {:%q:+.3f: %d:>6:}", 2_kmph);
    // s == "The speed is +2.000   kmph"
    

    Format specifications are shown here enclosed in columns: :+.3f: and :>6:.

    Terminating character : is necessary to detect the end of quantity/dimensions format specifier.

    The examples above do not pretend to be the only possible option of format string. They are more the ideas/proposals how could format look like. Just to express to power of it.
    The exact format of units formatting string is an open question.
    Another open question here is dimensions localization support. Most likely the library need to be extensible by user to provide dimensions string representations in any language other than English.

  3. The most complex one. Option (2) could be extended even more. To provide ability to have units conversions at formatting time.
    It has a few interesting use cases. Though it could be considered later - if option (2) is accepted.

A design note:
As formatting feature might become complicated - ideally if it will be "plug-able" to the units library.
Its implementation might be located in a separate optional header.
i.e.

#include <units/fmt.h>

The rest of library should be able to work without formatting header included.

from mp-units.

mpusz avatar mpusz commented on June 12, 2024

Thanks Aliaksander for investigating this subject. As of now, I did not have time to work on this. However, I strongly believe that we should strive to create something similar to http://wg21.link/P1361.

from mp-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.