Git Product home page Git Product logo

namedtype's Introduction

Build Status GitHub

A strong type is a type used in place of another type to carry specific meaning through its name.

This project experiments with strong types in C++. All components are in the namespace fluent. You can find a collection of blog posts explaining the rationale of the library and usages for strong types on Fluent C++.

become a patron

Basic usage

It central piece is the templated class NamedType, which can be used to declare a strong type with a typedef-like syntax:

using Width = NamedType<double, struct WidthTag>;
using Height = NamedType<double, struct HeightTag>;

which can be used to make interfaces more expressive and more robust. Note how the below constructor shows in which order it expects its parameters:

class Rectangle
{
public:
    Rectangle(Width width, Height height) : width_(width.get()), height_(height.get()) {}
    double getWidth() const { return width_; }
    double getHeight() const { return height_; }

private:
    double width_;
    double height_;
};

Strong types are about better expressing your intentions, both to the compiler and to other human developers.

Strong typing over generic types

This implementation of strong types can be used to add strong typing over generic or unknown types such as lambdas:

template<typename Function>
using Comparator = NamedType<Function, struct ComparatorTag>;

template <typename Function>
void performAction(Comparator<Function> comp)
{
    comp.get()();
}

performAction(make_named<Comparator>([](){ std::cout << "compare\n"; }));

Strong typing over references

The NamedType class is designed so that the following usage:

using FamilyNameRef = NamedType<std:string&, struct FamilyNameRefTag>;

behaves like a reference on an std::string, strongly typed.

Inheriting the underlying type functionalities

You can declare which functionalities should be inherited from the underlying type. So far, only basic operators are taken into account. For instance, to inherit from operator+ and operator<<, you can declare the strong type:

using Meter = NamedType<double, MeterTag, Addable, Printable>

There is one special skill, FunctionCallable, that lets the strong type be converted in the underlying type. This has the effect of removing the need to call .get() to get the underlying value. And MethodCallable enables operator-> on the strong type to invoke methods on the underlying type.

The skill Callable is the union of FunctionCallable and MethodCallable.

Named arguments

By their nature strong types can play the role of named parameters:

using FirstName = NamedType<std::string, struct FirstNameTag>;
using LastName = NamedType<std::string, struct LastNameTag>;

void displayName(FirstName const& theFirstName, LastName const& theLastName);

// Call site
displayName(FirstName("John"), LastName("Doe"));

But the nested type argument allows to emulate a named argument syntax:

using FirstName = NamedType<std::string, struct FirstNameTag>;
using LastName = NamedType<std::string, struct LastNameTag>;

static const FirstName::argument firstName;
static const LastName::argument lastName;

void displayName(FirstName const& theFirstName, LastName const& theLastName);

// Call site
displayName(firstName = "John", lastName = "Doe");

You can have a look at tests.cpp for usage examples.

become a patron

namedtype's People

Contributors

joboccara avatar pemessier avatar travnick avatar knatten avatar emmenlau avatar stanley00 avatar arvidn avatar ams21 avatar benhetherington avatar bebuch avatar anstow avatar idragnev avatar jonburla avatar juliancarrivick avatar mropert avatar shachlan avatar vincentzalzal avatar joris-planorama avatar oysteinbrandt avatar

Watchers

James Cloos avatar

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.