'strong types' a la boost

Charles Cooper via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Mar 14 07:55:04 PDT 2015


I was wondering what the idiomatic D way of implementing strong 
types. Boost has something along these lines using classes:
http://www.boost.org/doc/libs/1_37_0/boost/strong_typedef.hpp

When programming in C++ I find that the compiler does not 
necessarily generate good code with these types, and I usually 
use C++11 enum class, e.g.

enum class dollars_t : uint32_t {}
enum class cents_t : uint32_t {}
/* .. code e.g. for converting between cents and dollars .. */
void do_something_with_dollars(dollars_t) {} // this will fail to 
compile if you try to pass it cents_t or uint32_t

This is obviously a gross abuse of the enum class feature.

I think there is also a way of doing this (in C++) using 
templates a la std::chrono

But enough about C++. Is there an idiomatic way of doing this in 
D, if so what is it?

Thanks!
Charles


More information about the Digitalmars-d-learn mailing list